import { NextResponse } from "next/server"; import { z } from "zod"; import { registerUser } from "@/lib/service/auth"; import { AppError } from "@/lib/common/errors"; const registerSchema = z.object({ email: z.string().email(), password: z.string().min(8), name: z.string().optional(), }); export async function POST(req: Request) { let body: unknown; try { body = await req.json(); } catch { return NextResponse.json({ error: "invalid json" }, { status: 400 }); } const parsed = registerSchema.safeParse(body); if (!parsed.success) { return NextResponse.json( { error: "invalid input", details: parsed.error.flatten() }, { status: 400 }, ); } try { const user = await registerUser(parsed.data); return NextResponse.json({ id: user.id, email: user.email }, { status: 201 }); } catch (e) { if (e instanceof AppError) { return NextResponse.json({ error: e.message }, { status: e.statusCode }); } return NextResponse.json({ error: "internal error" }, { status: 500 }); } }