Spaces:
Sleeping
Sleeping
File size: 2,027 Bytes
64fa001 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | 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 })
}
}
|