"use client"; import React, { useState } from "react"; import { signInWithEmailAndPassword } from "firebase/auth"; import { auth } from "@/lib/firebase"; import { useRouter } from "next/navigation"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"; import Link from "next/link"; export default function LoginPage() { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); const router = useRouter(); const handleLogin = async (e: React.FormEvent) => { e.preventDefault(); setError(""); setLoading(true); try { const userCredential = await signInWithEmailAndPassword(auth, email, password); // Verify the user has a restaurant record before redirecting if (!userCredential.user?.email) { setError("Erro: Utilizador sem email válido."); return; } // Success — redirect to dashboard router.push("/"); } catch (err: any) { console.error("[Login Error]", err.code, err.message); // Map Firebase Auth error codes to user-friendly messages in Portuguese switch (err.code) { case "auth/user-not-found": setError("Esta conta não existe. Verifique o email ou registe-se."); break; case "auth/wrong-password": setError("Palavra-passe incorrecta. Tente novamente."); break; case "auth/invalid-email": setError("Email inválido. Verifique o formato do email."); break; case "auth/invalid-credential": setError("Credenciais inválidas. Verifique o email e a palavra-passe."); break; case "auth/too-many-requests": setError("Demasiadas tentativas falhadas. Aguarde alguns minutos e tente novamente."); break; case "auth/invalid-verification-id": setError("Sessão expirada. Por favor, recarregue a página e tente novamente."); break; case "auth/network-request-failed": setError("Erro de conexão. Verifique a sua ligação à internet."); break; case "auth/weak-password": setError("A palavra-passe é demasiado curta. Deve ter pelo menos 6 caracteres."); break; case "auth/popup-closed-by-user": // User closed the popup — no error needed break; default: setError("Erro ao entrar. Verifique as suas credenciais e tente novamente."); } } finally { setLoading(false); } }; return ( ReservaMesa Inicie sessão no seu painel de restaurante
{error && (
{error}
)}
setEmail(e.target.value)} required />
setPassword(e.target.value)} required />
Ainda não tem conta?{" "} Registe o seu restaurante
); }