Next.js 16, React 19, Monaco editor, Anthropic SDK, multi-provider AI, Wandbox Python execution, iframe HTML preview, SQLite auth + session persistence. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
31 lines
1.2 KiB
TypeScript
31 lines
1.2 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { db } from '@/db';
|
|
import { users } from '@/db/schema';
|
|
import { hashPassword, signToken, setAuthCookie } from '@/lib/auth';
|
|
import { eq } from 'drizzle-orm';
|
|
|
|
export async function POST(req: NextRequest) {
|
|
const { email, password } = await req.json();
|
|
|
|
if (!email || typeof email !== 'string' || !email.includes('@')) {
|
|
return NextResponse.json({ error: 'Valid email is required' }, { status: 400 });
|
|
}
|
|
if (!password || typeof password !== 'string' || password.length < 8) {
|
|
return NextResponse.json({ error: 'Password must be at least 8 characters' }, { status: 400 });
|
|
}
|
|
|
|
const existing = await db.select().from(users).where(eq(users.email, email)).get();
|
|
if (existing) {
|
|
return NextResponse.json({ error: 'Email already registered' }, { status: 409 });
|
|
}
|
|
|
|
const id = crypto.randomUUID();
|
|
const passwordHash = await hashPassword(password);
|
|
await db.insert(users).values({ id, email, passwordHash, createdAt: new Date().toISOString() });
|
|
|
|
const token = await signToken({ userId: id, email });
|
|
await setAuthCookie(token);
|
|
|
|
return NextResponse.json({ user: { id, email } });
|
|
}
|