import { FormEvent, useMemo, useState } from "react"; import { AlertCircle, Fish, Lock, Mail } from "lucide-react"; import { Button } from "./ui/button"; import { Input } from "./ui/input"; import { Label } from "./ui/label"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "./ui/card"; import { User } from "../types"; import { AuthApiError, login, preLogin } from "../services/authApi"; import { mapBackendUserToAppUser, persistAuthSession, } from "../services/authSession"; import { AuthLoginResult, FarmInfo } from "../services/authTypes"; interface LoginProps { onLogin: (user: User) => void; } type LoginStep = "credentials" | "farm-selection" | "otp-required"; const DEFAULT_OTP_MESSAGE = "OTP verification is required for this account. Complete OTP on the backend-supported channel."; const QUICK_LOGIN_PASSWORD = "FishFarm360!2026"; const QUICK_LOGIN_PRESETS = [ { key: "admin", email: "admin.test@fishfarm360.local" }, { key: "Warehouse Manager", email: "warehousemanager@fishfarm360.local", }, { key: "Technician", email: "technician.test@fishfarm360.local" }, { key: "Worker", email: "workeraccount@gmail.com" }, ]; const normalizeErrorMessage = (error: unknown): string => { if (error instanceof AuthApiError) { return error.message; } if (error instanceof Error && error.message.trim()) { return error.message; } return "Unable to sign in right now. Please try again."; }; export default function Login({ onLogin }: LoginProps) { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [farms, setFarms] = useState([]); const [selectedFarmId, setSelectedFarmId] = useState(""); const [step, setStep] = useState("credentials"); const [errorMessage, setErrorMessage] = useState(null); const [otpMessage, setOtpMessage] = useState(DEFAULT_OTP_MESSAGE); const [isSubmitting, setIsSubmitting] = useState(false); const stepDescription = useMemo(() => { if (step === "farm-selection") { return "Select the farm to continue login."; } if (step === "otp-required") { return "OTP verification is required before the session can be finalized."; } return "Sign in with your email and password."; }, [step]); const completeLogin = (result: AuthLoginResult): void => { if (result.requiresOTP) { setOtpMessage(result.message?.trim() || DEFAULT_OTP_MESSAGE); setStep("otp-required"); return; } if (!result.user || !result.token) { throw new Error("Login response is incomplete. Please try again."); } const appUser = mapBackendUserToAppUser(result.user); persistAuthSession(result, appUser); onLogin(appUser); }; const handleCredentialSubmit = async (event: FormEvent) => { event.preventDefault(); const normalizedEmail = email.trim(); if (!normalizedEmail || !password) { setErrorMessage("Email and password are required."); return; } setIsSubmitting(true); setErrorMessage(null); try { const response = await preLogin({ email: normalizedEmail, password, }); if (response.requiresFarmSelection) { if (!response.farms?.length) { throw new Error("No farms are available for this account."); } setFarms(response.farms); setSelectedFarmId(response.farms[0].id); setStep("farm-selection"); return; } if (!response.loginResult) { throw new Error("Pre-login completed but did not return login data."); } completeLogin(response.loginResult); } catch (error) { setErrorMessage(normalizeErrorMessage(error)); } finally { setIsSubmitting(false); } }; const handleFarmSubmit = async (event: FormEvent) => { event.preventDefault(); if (!selectedFarmId) { setErrorMessage("Please select a farm to continue."); return; } setIsSubmitting(true); setErrorMessage(null); try { const result = await login({ email: email.trim(), password, farmId: selectedFarmId, }); completeLogin(result); } catch (error) { setErrorMessage(normalizeErrorMessage(error)); } finally { setIsSubmitting(false); } }; const resetToCredentials = () => { setStep("credentials"); setErrorMessage(null); setOtpMessage(DEFAULT_OTP_MESSAGE); }; return (
FishFarm360 Smart Aquaculture Management Platform

{stepDescription}

{errorMessage && (
{errorMessage}
)} {step === "credentials" && (
setEmail(event.target.value)} disabled={isSubmitting} autoComplete="email" required />
setPassword(event.target.value)} disabled={isSubmitting} autoComplete="current-password" required />

Quick Login Presets

Password: {QUICK_LOGIN_PASSWORD}

{QUICK_LOGIN_PRESETS.map((preset) => ( ))}
)} {step === "farm-selection" && (
)} {step === "otp-required" && (

OTP required

{otpMessage}

)}
); }