import { useState, type FormEvent } from "react"; import { api } from "../api"; import { errorMessage, useAuth } from "../state"; import { Field } from "../components/ui"; import { Mark } from "../components/Mark"; import { Legal } from "../components/Legal"; export function Login() { const { refresh } = useAuth(); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [code, setCode] = useState(""); const [stage, setStage] = useState<"password" | "totp">("password"); const [error, setError] = useState(""); const [busy, setBusy] = useState(false); async function submit(e: FormEvent) { e.preventDefault(); setError(""); setBusy(true); try { if (stage === "password") { const r = await api.login(username, password); if (r.totpRequired) { setStage("totp"); return; } } else { await api.loginTotp(code); } await refresh(); } catch (err) { setError(errorMessage(err)); } finally { setBusy(false); } } return (