130 lines
4.5 KiB
TypeScript
130 lines
4.5 KiB
TypeScript
"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 (
|
|
<Card className="w-full">
|
|
<CardHeader className="space-y-1 text-center">
|
|
<CardTitle className="text-3xl text-primary">ReservaMesa</CardTitle>
|
|
<CardDescription>
|
|
Inicie sessão no seu painel de restaurante
|
|
</CardDescription>
|
|
</CardHeader>
|
|
<form onSubmit={handleLogin}>
|
|
<CardContent className="space-y-4">
|
|
{error && (
|
|
<div className="bg-destructive/15 text-destructive text-sm p-3 rounded-md">
|
|
{error}
|
|
</div>
|
|
)}
|
|
<div className="space-y-2">
|
|
<Label htmlFor="email">Email</Label>
|
|
<Input
|
|
id="email"
|
|
type="email"
|
|
placeholder="restaurante@exemplo.com"
|
|
value={email}
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
required
|
|
/>
|
|
</div>
|
|
<div className="space-y-2">
|
|
<div className="flex items-center justify-between">
|
|
<Label htmlFor="password">Palavra-passe</Label>
|
|
</div>
|
|
<Input
|
|
id="password"
|
|
type="password"
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
required
|
|
/>
|
|
</div>
|
|
</CardContent>
|
|
<CardFooter className="flex flex-col space-y-4">
|
|
<Button type="submit" className="w-full" disabled={loading}>
|
|
{loading ? "A entrar..." : "Entrar"}
|
|
</Button>
|
|
<div className="text-center text-sm text-muted-foreground">
|
|
Ainda não tem conta?{" "}
|
|
<Link href="/register" className="text-primary hover:underline">
|
|
Registe o seu restaurante
|
|
</Link>
|
|
</div>
|
|
</CardFooter>
|
|
</form>
|
|
</Card>
|
|
);
|
|
}
|