Spaces:
Sleeping
Sleeping
Download src/app/api/user/route.ts from veltrixcode/Iot: direct link, hf CLI and curl.
- Browser
- Download file 2.03 kB
-
https://huggingface.co/spaces/veltrixcode/Iot/resolve/main/src/app/api/user/route.ts
- Command line
-
hf download hf://spaces/veltrixcode/Iot/src/app/api/user/route.ts
-
curl -L -o route.ts https://huggingface.co/spaces/veltrixcode/Iot/resolve/main/src/app/api/user/route.ts
2.03 kB
| 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 }) | |
| } | |
| } | |