Buttons, fields, drop-downs, check boxes, tabs, dialogs and notices are Ant Design components; their tokens come from the theme variables (web/src/ui). Every factory board column has its own vertical scrollbar. ChatView rendered fewer hooks once its chat was deleted. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
59 lines
2.7 KiB
TypeScript
59 lines
2.7 KiB
TypeScript
import { Button, Input } from "antd";
|
|
import { useState } from "react";
|
|
import { post } from "../api";
|
|
import { useT } from "../i18n";
|
|
import type { Info } from "../types";
|
|
|
|
/** Sign-in, or creation of the first admin when no users exist yet. */
|
|
export function Login({ info, onSuccess }: { info: Info; onSuccess: () => void }) {
|
|
const t = useT();
|
|
const setup = info.setup_required;
|
|
const [form, setForm] = useState({ username: "", password: "", password2: "", code: "" });
|
|
const [error, setError] = useState<string>();
|
|
const [busy, setBusy] = useState(false);
|
|
|
|
const submit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setError(undefined);
|
|
if (setup && form.password !== form.password2) return setError(t("login.mismatch"));
|
|
setBusy(true);
|
|
try {
|
|
if (setup) await post("/auth/setup", { code: form.code, username: form.username, password: form.password });
|
|
else await post("/auth/login", { username: form.username, password: form.password });
|
|
onSuccess();
|
|
} catch (err: any) {
|
|
setError(err.status === 401 ? t(setup ? "login.invalidCode" : "login.invalid") : err.message);
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
};
|
|
|
|
const set = (k: keyof typeof form) => (e: React.ChangeEvent<HTMLInputElement>) => setForm({ ...form, [k]: e.target.value });
|
|
|
|
return (
|
|
<div className="login">
|
|
<form onSubmit={submit}>
|
|
<img src="/icon.svg" alt="" />
|
|
<h2 style={{ margin: 0 }}>CodeCraft</h2>
|
|
<div style={{ color: "var(--fg3)" }}>{setup ? t("login.setupPrompt") : t("login.prompt")}</div>
|
|
{setup && <Input autoFocus placeholder={t("login.setupCode")} value={form.code} onChange={set("code")} autoComplete="one-time-code" />}
|
|
<Input autoFocus={!setup} placeholder={t("login.username")} value={form.username} onChange={set("username")} autoComplete="username" autoCapitalize="none" />
|
|
<Input.Password placeholder={t("login.password")} value={form.password} onChange={set("password")} autoComplete={setup ? "new-password" : "current-password"} />
|
|
{setup && <Input.Password placeholder={t("login.passwordRepeat")} value={form.password2} onChange={set("password2")} autoComplete="new-password" />}
|
|
{error && <div style={{ color: "var(--red)" }}>{error}</div>}
|
|
<Button type="primary" htmlType="submit" disabled={busy || !form.username || !form.password}>
|
|
{setup ? t("login.createAdmin") : t("login.submit")}
|
|
</Button>
|
|
{!setup && info.oidc && (
|
|
<>
|
|
<div style={{ color: "var(--muted)", fontSize: 12 }}>{t("login.or")}</div>
|
|
<a className="button" href="/api/auth/oidc/start">
|
|
{info.oidc.label}
|
|
</a>
|
|
</>
|
|
)}
|
|
</form>
|
|
</div>
|
|
);
|
|
}
|