/profile: two-card layout with personal info form (name, birth date, email, contact) and password change form, each with independent actions. /users/[id]: read-only public view showing name, username, email, contact with avatar circle initials. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
55 lines
1.9 KiB
TypeScript
55 lines
1.9 KiB
TypeScript
import { fail } from '@sveltejs/kit';
|
|
import type { PageServerLoad, Actions } from './$types';
|
|
import { createApiClient } from '$lib/api.server';
|
|
import { getErrorMessage } from '$lib/errors';
|
|
|
|
export const load: PageServerLoad = async ({ locals }) => {
|
|
return { user: locals.user };
|
|
};
|
|
|
|
export const actions: Actions = {
|
|
updateProfile: async ({ request, fetch }) => {
|
|
const formData = await request.formData();
|
|
const body = {
|
|
firstName: formData.get('firstName')?.toString().trim() || undefined,
|
|
lastName: formData.get('lastName')?.toString().trim() || undefined,
|
|
birthDate: (formData.get('birthDate')?.toString() || undefined) as string | undefined,
|
|
email: formData.get('email')?.toString().trim() || undefined,
|
|
contact: formData.get('contact')?.toString().trim() || undefined
|
|
};
|
|
|
|
const api = createApiClient(fetch);
|
|
const result = await api.PUT('/api/users/me', { body });
|
|
|
|
if (!result.response.ok) {
|
|
const code = (result.error as unknown as { code?: string })?.code;
|
|
return fail(result.response.status, { updateError: getErrorMessage(code) });
|
|
}
|
|
|
|
return { updateSuccess: true };
|
|
},
|
|
|
|
changePassword: async ({ request, fetch }) => {
|
|
const formData = await request.formData();
|
|
const currentPassword = formData.get('currentPassword')?.toString() ?? '';
|
|
const newPassword = formData.get('newPassword')?.toString() ?? '';
|
|
const confirmPassword = formData.get('confirmPassword')?.toString() ?? '';
|
|
|
|
if (newPassword !== confirmPassword) {
|
|
return fail(400, { passwordError: 'PASSWORDS_DO_NOT_MATCH' });
|
|
}
|
|
|
|
const api = createApiClient(fetch);
|
|
const result = await api.POST('/api/users/me/password', {
|
|
body: { currentPassword, newPassword }
|
|
});
|
|
|
|
if (!result.response.ok) {
|
|
const code = (result.error as unknown as { code?: string })?.code;
|
|
return fail(result.response.status, { passwordError: getErrorMessage(code) });
|
|
}
|
|
|
|
return { passwordSuccess: true };
|
|
}
|
|
};
|