import type { FormEvent } from "react"; import { useState } from "react"; import { api, errorMessage } from "../api"; import { Alert } from "../components/ui"; import { ThemeSwitch } from "../theme"; import type { AdminSession } from "../types"; export function LoginPage({ onLogin }: { onLogin: (session: AdminSession) => void }) { // Deliberately not pre-filled: the built-in operator is a default name, not a // default identity, and typing it is the difference between choosing to use // it and drifting into it. The server rejects a blank username either way. const [username, setUsername] = useState(""); const [secret, setSecret] = useState(""); const [error, setError] = useState(""); const [busy, setBusy] = useState(false); async function submit(event: FormEvent) { event.preventDefault(); setBusy(true); setError(""); try { // The login answer carries the permission set and the CSRF token; api.login // remembers the token, the session state keeps the rights. const result = await api.login(secret, username); onLogin({ actor: result.actor, permissions: result.permissions ?? [] }); } catch (err) { setError(errorMessage(err)); } finally { setBusy(false); } } return (
); }