feat(frontend): add profile page and public user profile page
/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>
This commit is contained in:
54
frontend/src/routes/profile/+page.server.ts
Normal file
54
frontend/src/routes/profile/+page.server.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
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 };
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user