49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { getAuth, createUserWithEmailAndPassword, updateProfile } from 'firebase/auth';
|
|
import { app } from '@/lib/firebase';
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const { email, password, name } = await request.json();
|
|
|
|
if (!email || !password || !name) {
|
|
return NextResponse.json(
|
|
{ error: 'Missing email, password or name' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const auth = getAuth(app);
|
|
|
|
const userCredential = await createUserWithEmailAndPassword(auth, email, password);
|
|
|
|
if (auth.currentUser) {
|
|
await updateProfile(auth.currentUser, { displayName: name });
|
|
}
|
|
|
|
const user = userCredential.user;
|
|
|
|
// Firebase client SDK already keeps the session via cookies/local storage on client.
|
|
// For MVP, we just return basic user info.
|
|
return NextResponse.json(
|
|
{
|
|
user: {
|
|
uid: user.uid,
|
|
email: user.email,
|
|
name: user.displayName,
|
|
},
|
|
},
|
|
{ status: 201 }
|
|
);
|
|
} catch (error: any) {
|
|
console.error('Register error:', error);
|
|
|
|
const message =
|
|
error?.code === 'auth/email-already-in-use'
|
|
? 'Email already in use'
|
|
: 'Registration failed';
|
|
|
|
return NextResponse.json({ error: message }, { status: 400 });
|
|
}
|
|
}
|