"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; import { toast } from "sonner"; import { api, AdminError, type Me } from "@/lib/api"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; export default function LoginPage() { const router = useRouter(); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [otp, setOtp] = useState(""); const [stage, setStage] = useState<"credentials" | "otp">("credentials"); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); // Once authenticated, only admins may proceed. Non-admins are signed back // out so an ordinary account can't sit in an authenticated-but-forbidden // state against the admin origin. async function gateAdmin(user: Me) { if (user.role === "admin") { router.replace("/users"); return; } await api.logout().catch(() => {}); setError("This account doesn't have admin access."); setStage("credentials"); } async function onCredentials(e: React.FormEvent) { e.preventDefault(); setError(null); setBusy(true); try { const res = await api.login(email.trim(), password); if (res.kind === "otp_required") { setStage("otp"); toast("We emailed you a verification code."); } else { await gateAdmin(res.user); } } catch (err) { setError(err instanceof AdminError ? err.message : "Could not sign in."); } finally { setBusy(false); } } async function onOtp(e: React.FormEvent) { e.preventDefault(); setError(null); setBusy(true); try { const user = await api.verifyLoginOtp(email.trim(), password, otp.trim()); await gateAdmin(user); } catch (err) { setError(err instanceof AdminError ? err.message : "Invalid code."); } finally { setBusy(false); } } return ( {stage === "credentials" ? (
setEmail(e.target.value)} /> setPassword(e.target.value)} /> {error ?

{error}

: null}

Forgot your password?

) : (

Enter the verification code we emailed to {email}.

setOtp(e.target.value)} /> {error ?

{error}

: null}
)}
); } export function AuthShell({ title, children, }: { title: string; children: React.ReactNode; }) { return (

Prabhupāda Says

{title}

{children}
); } export function Field({ label, children, }: { label: string; children: React.ReactNode; }) { return ( ); }