This commit is contained in:
asabizanjo
2025-12-11 01:05:24 +00:00
parent c713d58f98
commit 423ce1bc6d
88 changed files with 4081 additions and 122 deletions

View File

@@ -0,0 +1,42 @@
import { NextResponse } from "next/server";
import { prisma } from "@/lib/db";
import {
createSessionToken,
hashPassword,
setSessionCookie,
} from "@/lib/auth";
export async function POST(req: Request) {
const body = await req.json().catch(() => null);
const email = (body?.email as string | undefined)?.toLowerCase()?.trim();
const password = body?.password as string | undefined;
if (!email || !password || password.length < 6) {
return NextResponse.json(
{ error: "Email and password (min 6 chars) are required." },
{ status: 400 }
);
}
const existing = await prisma.user.findUnique({ where: { email } });
if (existing) {
return NextResponse.json(
{ error: "Email is already registered." },
{ status: 400 }
);
}
const passwordHash = await hashPassword(password);
const user = await prisma.user.create({
data: { email, passwordHash },
});
const token = await createSessionToken({ userId: user.id, email: user.email });
await setSessionCookie(token);
return NextResponse.json({
ok: true,
user: { id: user.id, email: user.email },
});
}