"use client"; import React, { useState } from "react"; import Link from "next/link"; import { Mail, Lock, ShieldCheck, KeyRound, AlertCircle, } from "lucide-react"; import { useTranslations } from "../../../../i18n/provider"; import { Card } from "../../../../components/ui/Card"; import { Button } from "../../../../components/ui/Button"; export default function UserLoginPage() { const t = useTranslations(); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [mfaCode, setMfaCode] = useState(""); const [challengeToken, setChallengeToken] = useState(null); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); const handleLogin = async (e: React.FormEvent) => { e.preventDefault(); setError(null); setLoading(true); try { if (challengeToken) { // Step 2: Verify TOTP code const res = await fetch("/api/v1/auth/mfa/verify", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ challenge_token: challengeToken, code: mfaCode }), }); const data = await res.json(); if (!res.ok) throw new Error(data.message || "Two-factor verification failed"); window.location.href = "/user/mail/inbox"; return; } // Step 1: Password authentication const res = await fetch("/api/v1/auth/user/login", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email, password }), }); const data = await res.json(); if (!res.ok) throw new Error(data.message || "Invalid credentials"); if (data.mfa_required) { setChallengeToken(data.challenge_token); } else { window.location.href = "/user/mail/inbox"; } } catch (err: unknown) { setError(err instanceof Error ? err.message : "Ocorreu um erro ao autenticar"); } finally { setLoading(false); } }; return (
{/* Background Ambient Glow */}

{t("auth.userLoginTitle")}

{t("common.appName")} · {t("common.userPortal")}

{error && (
{error}
)}
{!challengeToken ? ( <>
setEmail(e.target.value)} placeholder="user@domain.com" required className="w-full pl-10 pr-4 py-2.5 rounded-xl bg-slate-900/90 border border-slate-800 text-white placeholder-slate-500 focus:outline-none focus:border-indigo-500 transition-colors" />
setPassword(e.target.value)} placeholder="************" required className="w-full pl-10 pr-4 py-2.5 rounded-xl bg-slate-900/90 border border-slate-800 text-white placeholder-slate-500 focus:outline-none focus:border-indigo-500 transition-colors" />
) : (
{t("auth.mfaRequired")}
setMfaCode(e.target.value)} placeholder="123456" maxLength={6} required className="w-full pl-10 pr-4 py-3 rounded-xl bg-slate-900/90 border border-slate-800 text-white text-center tracking-widest text-lg font-mono focus:outline-none focus:border-indigo-500 transition-colors" />
)}
); }