import { auth } from "@/auth" import { prisma } from "@/lib/prisma" import { NextResponse } from "next/server" import { z } from "zod" export async function PUT(req: Request) { try { const session = await auth() if (!session?.user?.id) { return new NextResponse("Unauthorized", { status: 401 }) } const body = await req.json() const { name, username } = z .object({ name: z.string().min(2).max(50), username: z.string().min(3).max(20).regex(/^[a-zA-Z0-9_]+$/, "Username must be alphanumeric"), }) .parse(body) // Check if username is taken (if changed) if (username !== session.user.username) { const existingUser = await prisma.user.findUnique({ where: { username }, }) if (existingUser) { return new NextResponse("Username already taken", { status: 409 }) } } const updatedUser = await prisma.user.update({ where: { id: session.user.id }, data: { name, username }, }) return NextResponse.json(updatedUser) } catch (error) { if (error instanceof z.ZodError) { return new NextResponse("Invalid data", { status: 422 }) } console.error("[USER_UPDATE]", error) return new NextResponse("Internal Error", { status: 500 }) } } export async function DELETE(req: Request) { try { const session = await auth() if (!session?.user?.id) { return new NextResponse("Unauthorized", { status: 401 }) } // Delete user (Cascade will handle Generations, Accounts, Sessions) await prisma.user.delete({ where: { id: session.user.id }, }) return new NextResponse(null, { status: 204 }) } catch (error) { console.error("[USER_DELETE]", error) return new NextResponse("Internal Error", { status: 500 }) } }