Files
tsumugu/frontend/src/pages/Login.jsx
T
LarsandCursor 97bffc6175
Deploy Development / deploy (push) Successful in 25s
Test Suite / pytest-backend (push) Successful in 56s
Test Suite / smoke-dev (push) Successful in 0s
Test Suite / frontend-build (push) Successful in 8s
Halte die gewaehlte Inhaltsliste beim Oeffnen und mache Typ sowie das Logo auf dunkler Flaeche aus der Lieferung erkennbar.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-17 15:50:05 +02:00

134 lines
4.9 KiB
React

import { useContext, useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import { api, applySession } from "../api.js";
import { SessionContext } from "../session.js";
import { Mark } from "./icons.jsx";
export function Login() {
const { setSession } = useContext(SessionContext);
const navigate = useNavigate();
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [error, setError] = useState("");
const [demo, setDemo] = useState(null);
const [bootstrap, setBootstrap] = useState(undefined);
const [displayName, setDisplayName] = useState("");
const [pending, setPending] = useState(false);
const [introSettled, setIntroSettled] = useState(false);
const markMotion = pending ? "busy" : introSettled ? "none" : "intro";
useEffect(() => {
api("/api/v1/auth/demo")
.then(setDemo)
.catch(() => setDemo(null));
api("/api/v1/auth/bootstrap")
.then(setBootstrap)
.catch(() => setBootstrap(null));
}, []);
async function submit(event) {
event.preventDefault();
setIntroSettled(true);
setPending(true);
setError("");
try {
const data = await api("/api/v1/auth/login", { method: "POST", body: { email, password } });
applySession(data);
setSession(data);
navigate("/");
} catch (err) {
setError(err.message);
} finally {
setPending(false);
}
}
async function createOperator(event) {
event.preventDefault();
setIntroSettled(true);
setPending(true);
setError("");
try {
const data = await api("/api/v1/auth/bootstrap", {
method: "POST",
body: { email, display_name: displayName, password },
});
applySession(data);
setSession(data);
navigate("/");
} catch (err) {
setError(err.message);
} finally {
setPending(false);
}
}
return (
<main className="login">
<section className="card login-card">
<p className="login-mark"><Mark size={96} motion={markMotion} onIntroComplete={() => setIntroSettled(true)} /></p>
<p className="eyebrow">Jinkendo</p>
<h1>Tsumugu</h1>
<p className="lead">Akten, Vorgänge und Fristen an einem Ort. Die erste Lieferung organisiert und verfolgt.</p>
{bootstrap === undefined ? null : bootstrap?.available ? (
<form onSubmit={createOperator} className="stack" aria-busy={pending}>
<h2>Betreiberkonto</h2>
<p className="muted">Das erste Konto wird Betreiber und kann danach weitere Nutzer anlegen.</p>
<label>
Name
<input value={displayName} onChange={(event) => setDisplayName(event.target.value)} required maxLength={120} />
</label>
<label>
E-Mail
<input type="email" autoComplete="username" value={email} onChange={(event) => setEmail(event.target.value)} required />
</label>
<label>
Passwort
<input type="password" autoComplete="new-password" value={password} onChange={(event) => setPassword(event.target.value)} required minLength={10} />
</label>
{error ? <p className="error">{error}</p> : null}
<button type="submit" className="btn-full" disabled={pending} aria-busy={pending}>{pending ? "Wird angelegt …" : "Betreiberkonto anlegen"}</button>
</form>
) : (
<form onSubmit={submit} className="stack" aria-busy={pending}>
<label>
E-Mail
<input type="email" autoComplete="username" value={email} onChange={(event) => setEmail(event.target.value)} required />
</label>
<label>
Passwort
<input type="password" autoComplete="current-password" value={password} onChange={(event) => setPassword(event.target.value)} required />
</label>
{error ? <p className="error">{error}</p> : null}
<button type="submit" className="btn-full" disabled={pending} aria-busy={pending}>{pending ? "Anmeldung …" : "Anmelden"}</button>
</form>
)}
{demo ? (
<div className="demo">
<h2>Beispieldaten</h2>
<p>Zwei synthetische Konten. Private Inhalte von Ada bleiben für Ben unsichtbar.</p>
<ul>
{demo.accounts.map((account) => (
<li key={account.email}>
<button
type="button"
className="linkish"
onClick={() => {
setEmail(account.email);
setPassword(account.password);
}}
>
{account.display_name}
</button>
<span>{account.email}</span>
<code>{account.password}</code>
</li>
))}
</ul>
</div>
) : null}
</section>
</main>
);
}