Scrum4Me/actions/profile.ts
janpeter visser 1ff894a6c0 feat: gebruikersprofiel met avatar, bio en uitgebreide beschrijving
- Schema: bio (160), bio_detail (2000) en avatar_data (bytea) op User
- POST /api/profile/avatar: validatie MIME-type + max 12 MB vóór verwerking,
  Sharp resize naar max 700x700 (fit inside), output WebP q85, opgeslagen als bytea in Neon
- GET /api/profile/avatar: serveert avatar met Cache-Control private 1u
- updateProfileAction: slaat bio en bio_detail op via Server Action + Zod
- ProfileEditor client component: avatar preview, upload met client-side validatie,
  bio-velden met tekenlimieten
- Settings page: profiel-sectie bovenaan, uitgeschakeld voor demo-gebruiker
- next.config: sharp als serverExternalPackage

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-25 13:30:38 +02:00

40 lines
1.2 KiB
TypeScript

'use server'
import { revalidatePath } from 'next/cache'
import { cookies } from 'next/headers'
import { getIronSession } from 'iron-session'
import { z } from 'zod'
import { prisma } from '@/lib/prisma'
import { SessionData, sessionOptions } from '@/lib/session'
async function getSession() {
return getIronSession<SessionData>(await cookies(), sessionOptions)
}
const profileSchema = z.object({
bio: z.string().max(160).optional(),
bio_detail: z.string().max(2000).optional(),
})
export async function updateProfileAction(_prevState: unknown, formData: FormData) {
const session = await getSession()
if (!session.userId) return { error: 'Niet ingelogd' }
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus' }
const parsed = profileSchema.safeParse({
bio: (formData.get('bio') as string) || undefined,
bio_detail: (formData.get('bio_detail') as string) || undefined,
})
if (!parsed.success) return { error: parsed.error.flatten().fieldErrors }
await prisma.user.update({
where: { id: session.userId },
data: {
bio: parsed.data.bio ?? null,
bio_detail: parsed.data.bio_detail ?? null,
},
})
revalidatePath('/settings')
return { success: true }
}