Files
holacompi/app/api/auth/login/route.ts
T
Haitham Khalifa b538d84e17 Initial commit
2026-02-16 12:18:06 +01:00

47 lines
1.2 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import { getAuth, signInWithEmailAndPassword } from 'firebase/auth';
import { app } from '@/lib/firebase';
export async function POST(request: NextRequest) {
try {
const { email, password } = await request.json();
if (!email || !password) {
return NextResponse.json(
{ error: 'Missing email or password' },
{ status: 400 }
);
}
const auth = getAuth(app);
const userCredential = await signInWithEmailAndPassword(auth, email, password);
const user = userCredential.user;
return NextResponse.json(
{
user: {
uid: user.uid,
email: user.email,
name: user.displayName,
},
},
{ status: 200 }
);
} catch (error: any) {
console.error('Login error:', error);
let message = 'Login failed';
if (error?.code === 'auth/user-not-found') {
message = 'No user found with that email';
} else if (error?.code === 'auth/wrong-password') {
message = 'Incorrect password';
} else if (error?.code === 'auth/invalid-credential') {
message = 'Invalid credentials';
}
return NextResponse.json({ error: message }, { status: 400 });
}
}