feat(ST-507): persist email in updateProfileAction with Zod validation

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Janpeter Visser 2026-04-26 19:37:49 +02:00
parent 1187117e05
commit 5ed3645ecb

View file

@ -12,6 +12,7 @@ async function getSession() {
} }
const profileSchema = z.object({ const profileSchema = z.object({
email: z.string().trim().email('Ongeldig e-mailadres').max(254).optional(),
bio: z.string().max(160).optional(), bio: z.string().max(160).optional(),
bio_detail: z.string().max(2000).optional(), bio_detail: z.string().max(2000).optional(),
}) })
@ -22,18 +23,32 @@ export async function updateProfileAction(_prevState: unknown, formData: FormDat
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus' } if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus' }
const parsed = profileSchema.safeParse({ const parsed = profileSchema.safeParse({
email: (formData.get('email') as string)?.trim() || undefined,
bio: (formData.get('bio') as string) || undefined, bio: (formData.get('bio') as string) || undefined,
bio_detail: (formData.get('bio_detail') as string) || undefined, bio_detail: (formData.get('bio_detail') as string) || undefined,
}) })
if (!parsed.success) return { error: parsed.error.flatten().fieldErrors } if (!parsed.success) return { error: parsed.error.flatten().fieldErrors }
try {
await prisma.user.update({ await prisma.user.update({
where: { id: session.userId }, where: { id: session.userId },
data: { data: {
email: parsed.data.email ?? null,
bio: parsed.data.bio ?? null, bio: parsed.data.bio ?? null,
bio_detail: parsed.data.bio_detail ?? null, bio_detail: parsed.data.bio_detail ?? null,
}, },
}) })
} catch (err: unknown) {
if (
typeof err === 'object' &&
err !== null &&
'code' in err &&
(err as { code: string }).code === 'P2002'
) {
return { error: 'Dit e-mailadres is al in gebruik' }
}
throw err
}
revalidatePath('/settings') revalidatePath('/settings')
return { success: true } return { success: true }