This commit is contained in:
2026-03-30 15:44:11 -05:00
parent 09cdce350d
commit a0efc6c628
6 changed files with 158 additions and 70 deletions
+59 -1
View File
@@ -1,6 +1,9 @@
import type { PageServerLoad } from '$types';
import sql from '$lib/db/db.server';
import type { User } from '$lib/types/user';
import type { User, UserSettings } from '$lib/types/user';
import { type Actions, error, fail } from '@sveltejs/kit';
import { getFormString, getRequiredFormString } from '$lib/shared';
import bcrypt from 'bcrypt';
export const load: PageServerLoad = async ({ locals }) => {
if (!locals || !locals.user) {
@@ -15,3 +18,58 @@ export const load: PageServerLoad = async ({ locals }) => {
return { userData };
};
export const actions: Actions = {
default: async ({ request, url, locals, params }) => {
if (!locals || !locals.user) {
throw error(403, 'Need to be logged in!');
}
const data = await request.formData();
let name: string;
let email: string;
let newPassword: string | null;
let retypeNewPassword: string | null;
let staleItemDays: number;
let notifyAllApprovedInquiries: boolean;
let notifyAllTurnedInInquiries: boolean;
try {
name = getRequiredFormString(data, 'name');
email = getRequiredFormString(data, 'email');
newPassword = getFormString(data, 'newPassword');
retypeNewPassword = getFormString(data, 'retypeNewPassword');
staleItemDays = parseInt(getRequiredFormString(data, 'staleItemDays'));
notifyAllApprovedInquiries = getFormString(data, 'notifyAllApprovedInquiries') == 'on';
notifyAllTurnedInInquiries = getFormString(data, 'notifyAllTurnedInInquiries') == 'on';
if (newPassword !== retypeNewPassword) {
fail(400, { password: 'New passwords dont match!' });
}
const passwordHash = await bcrypt.hash(newPassword!, 12);
const settings: UserSettings = {
staleItemDays,
notifyAllApprovedInquiries,
notifyAllTurnedInInquiries
};
return await sql`
UPDATE users SET name = ${name},
email = ${email},
password_hash = ${passwordHash},
settings = ${settings.toString()}
WHERE id = ${locals.user.id}
RETURNING *;
`;
} catch (e) {
return fail(400, {
message: e instanceof Error ? e.message : 'Unknown error occurred',
success: false
});
}
return { success: true };
}
} satisfies Actions;