Files
FlorteSite/src/app/login/page.tsx
T
faumaray 75e03b42f7 init
Signed-off-by: faumaray <faumaray@gmail.com>
2026-05-04 08:30:15 +03:00

95 lines
2.8 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import Link from "next/link";
import { useFlorte } from "@/components/ClientProviders";
export default function LoginPage() {
const router = useRouter();
const { refresh, t } = useFlorte();
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [error, setError] = useState<string | null>(null);
const [busy, setBusy] = useState(false);
const [needsSetup, setNeedsSetup] = useState(false);
useEffect(() => {
fetch("/api/auth/setup")
.then((r) => r.json())
.then((d) => {
if (d.needsSetup) {
setNeedsSetup(true);
router.replace("/setup");
}
});
}, [router]);
const submit = async (e: React.FormEvent) => {
e.preventDefault();
setError(null);
setBusy(true);
const res = await fetch("/api/auth/login", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, password }),
});
setBusy(false);
if (!res.ok) {
const j = await res.json().catch(() => ({}));
setError(j.error ?? "Login failed");
return;
}
await refresh();
const j = await res.json();
router.push(j.role === "player" ? "/portal" : "/");
router.refresh();
};
if (needsSetup) return null;
return (
<div className="flo-shell">
<form className="flo-auth-card" onSubmit={submit}>
<h1>{t("auth_welcome_back")}</h1>
<p className="flo-auth-sub">{t("auth_welcome_back_sub")}</p>
{error && <div className="flo-error">{error}</div>}
<div className="flo-auth-row">
<label>{t("auth_username")}</label>
<input
className="flo-input"
value={username}
onChange={(e) => setUsername(e.target.value)}
autoFocus
required
/>
</div>
<div className="flo-auth-row">
<label>{t("auth_password")}</label>
<input
className="flo-input"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
/>
</div>
<div className="flo-auth-actions">
<button type="submit" className="flo-btn" disabled={busy}>
{busy ? t("auth_signing_in") : t("auth_sign_in")}
</button>
<Link href="/setup" className="flo-link">
First-run setup →
</Link>
</div>
<div className="flo-auth-foot">
{t("auth_no_account")}{" "}
<Link href="/register" className="flo-link">
{t("auth_register")} →
</Link>
</div>
</form>
</div>
);
}