import { type FormEvent, useState } from "react"; import { Alert, Box, Button, Center, Group, Image, PasswordInput, PinInput, Stack, Text, TextInput, Title, } from "@mantine/core"; import { AlertCircle, ArrowLeft } from "lucide-react"; import { useNavigate } from "react-router-dom"; import { useAuth } from "@/auth/useAuth"; import AuthShell from "@/components/auth/AuthShell"; import { extractApiError } from "@/utils/result"; /** Normalise Ethiopian local phone (09…/07…) to E.164; pass email through unchanged. */ const normaliseIdentifier = (raw: string): string => { const v = raw.trim(); const digits = v.replace(/\D/g, ""); if (digits.length >= 9 && (v.startsWith("0") || v.startsWith("+251"))) { const local = digits.startsWith("251") ? digits.slice(3) : digits.replace(/^0/, ""); return `+251${local}`; } return v.toLowerCase(); }; const EDR_LOGO = "/assets/logo.svg"; const LoginPage = () => { const navigate = useNavigate(); const { login, verifyMfa } = useAuth(); const [identifier, setIdentifier] = useState(""); const [password, setPassword] = useState(""); const [otp, setOtp] = useState(""); const [needsMfa, setNeedsMfa] = useState(false); const [submitting, setSubmitting] = useState(false); const [normalizedIdentifier, setNormalizedIdentifier] = useState(""); const [error, setError] = useState(null); const handleSubmit = async (event: FormEvent) => { event.preventDefault(); setSubmitting(true); setError(null); try { const normalized = normaliseIdentifier(identifier); setNormalizedIdentifier(normalized); const result = await login({ email: normalized, password }); if (result.mfaRequired) { setNeedsMfa(true); return; } // navigate("/dashboard/overview", { replace: true }); } catch (err) { setError(extractApiError(err).message); } finally { setSubmitting(false); } }; const handleVerifyMfa = async (event: FormEvent) => { event.preventDefault(); setSubmitting(true); setError(null); try { await verifyMfa({ email: normalizedIdentifier, otp: otp.trim() }); navigate("/dashboard/overview", { replace: true }); } catch (err) { setError(extractApiError(err).message); } finally { setSubmitting(false); } }; const loginForm = (
EDR Freight
Welcome back! Log in to access the freight backoffice & explore all logistics resources. setIdentifier(event.target.value)} /> setPassword(event.target.value)} /> {error ? ( }> {error} ) : null}
); const mfaForm = (
EDR Freight
Multi-factor verification We sent a verification code to{" "} {normalizedIdentifier} . Enter it below to complete sign in. Verification code {error ? ( }> {error} ) : null}
); return {!needsMfa ? loginForm : mfaForm}; }; export default LoginPage;