"use client"; import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { ApiError, login, me } from "@/lib/api"; export default function LoginPage() { const router = useRouter(); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); const [bootChecking, setBootChecking] = useState(true); // If already signed in, skip straight to the customer browser. useEffect(() => { let alive = true; me() .then(() => router.replace("/inicio")) .catch(() => { if (alive) setBootChecking(false); }); return () => { alive = false; }; }, [router]); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setError(null); setSubmitting(true); try { await login(email.trim(), password); router.replace("/inicio"); } catch (err) { if (err instanceof ApiError && err.status === 401) { setError("Correo o contraseña incorrectos"); } else if (err instanceof ApiError && err.status === 0) { setError(err.message); } else { setError("No se pudo iniciar sesión. Inténtalo de nuevo."); } setSubmitting(false); } } if (bootChecking) { return (
); } return (
{/* Brand / narrative panel */} {/* Form panel */}

Acceso del personal

Iniciar sesión

Ingresa con tu cuenta para continuar.

{error && (
{error}
)}
); }