"use client"; import { useMemo, useState } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; import Image from "next/image"; import { Button } from "@/components/ui/button"; import { Card, CardContent, } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Checkbox } from "@/components/ui/checkbox"; import { Briefcase, AlertCircle, Eye, EyeOff, User as UserIcon, Lock, Building2, } from "lucide-react"; import { login } from "@/lib/auth"; import { Alert, AlertDescription } from "@/components/ui/alert"; import { motion } from "framer-motion"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { useTranslation } from "@/lib/i18n"; type LoginFormData = { email: string; password: string; rememberMe?: boolean; }; export default function LoginPage() { const router = useRouter(); const { t } = useTranslation(); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); const [showPassword, setShowPassword] = useState(false); const loginSchema = useMemo(() => z.object({ email: z.string().min(3, t("auth.login.validation.username")), password: z.string().min(3, t("auth.login.validation.password")), rememberMe: z.boolean().optional(), }), [t]); const { register, handleSubmit, formState: { errors }, } = useForm({ resolver: zodResolver(loginSchema), defaultValues: { email: "", password: "", rememberMe: false, }, }); const handleLogin = async (data: LoginFormData) => { setError(""); setLoading(true); try { // Login sem passar role, o backend decide console.log('🚀 [LOGIN FRONT] Tentando login com:', data.email); const user = await login(data.email, data.password); console.log('✅ [LOGIN FRONT] Sucesso:', user); if (user) { // Se "lembrar de mim" estiver marcado, salvar no localStorage if (data.rememberMe) { localStorage.setItem("rememberedEmail", data.email); } router.push("/dashboard"); } else { setError(t("auth.login.errors.invalidCredentials")); } } catch (err: any) { console.error('🔥 [LOGIN FRONT] Erro no login:', err); console.error('🔥 [LOGIN FRONT] Detalhes:', err.response?.data || err.message); const errorMessage = err.message; if (errorMessage === "AUTH_INVALID_CREDENTIALS") { setError(t("auth.login.errors.invalidCredentials")); } else if (errorMessage === "AUTH_SERVER_ERROR") { setError(t("auth.login.errors.serverError")); } else { setError(t("auth.login.errors.generic")); } } finally { setLoading(false); } }; const onSubmit = (data: LoginFormData) => { handleLogin(data); }; return (
{/* Left Side - Branding */}
GoHorseJobs

{t("auth.login.hero.title")}

{t("auth.login.hero.subtitle")}

{t("auth.login.hero.bulletProfile")}
{t("auth.login.hero.bulletCompanies")}
{t("auth.login.hero.bulletJobs")}
{/* Right Side - Login Form */}

{t("auth.login.title")}

{t("auth.login.subtitle")}

{error && ( {error} )}
{errors.email && (

{errors.email.message}

)}
{errors.password && (

{errors.password.message}

)}
{t("auth.login.forgotPassword")}
{t("auth.login.backHome")}
); }