predictor / apps /web /components /Gate.tsx
pengpeng9099's picture
Deploy World Cup 2026 Predictor (docker space)
1790bde verified
Raw History Blame Contribute Delete
2.69 kB
"use client";
import { useEffect, useState } from "react";
import { api } from "@/lib/api";
const KEY = "wc_gate_ok";
export default function Gate({ children }: { children: React.ReactNode }) {
const [state, setState] = useState<"checking" | "locked" | "open">("checking");
const [pw, setPw] = useState("");
const [err, setErr] = useState("");
const [busy, setBusy] = useState(false);
useEffect(() => {
// already unlocked this browser?
if (typeof window !== "undefined" && localStorage.getItem(KEY) === "1") {
setState("open");
return;
}
api<{ required: boolean }>("/api/auth/required")
.then((r) => setState(r.required ? "locked" : "open"))
.catch(() => setState("open")); // if check fails, don't lock people out
}, []);
const submit = async (e: React.FormEvent) => {
e.preventDefault();
setBusy(true);
setErr("");
try {
const r = await api<{ ok: boolean }>("/api/auth", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ password: pw }),
});
if (r.ok) {
localStorage.setItem(KEY, "1");
setState("open");
} else {
setErr("密码不正确");
}
} catch {
setErr("验证失败,请确认后端已启动");
} finally {
setBusy(false);
}
};
if (state === "open") return <>{children}</>;
if (state === "checking")
return (
<div className="flex min-h-screen items-center justify-center text-white/40">载入中…</div>
);
return (
<div className="flex min-h-screen items-center justify-center px-4">
<form onSubmit={submit} className="card w-full max-w-sm p-8 text-center">
<div className="text-5xl">⚽</div>
<h1 className="mt-3 text-xl font-black">
世界杯 <span className="text-gold">预测引擎</span>
</h1>
<p className="mt-1 text-sm text-white/40">本站仅供小范围使用,请输入访问密码</p>
<input
type="password"
value={pw}
autoFocus
onChange={(e) => setPw(e.target.value)}
placeholder="访问密码"
className="mt-5 w-full rounded-lg border border-white/10 bg-ink-card px-4 py-2.5 text-center outline-none focus:border-gold"
/>
{err && <div className="mt-2 text-sm text-red-400">{err}</div>}
<button
type="submit"
disabled={busy || !pw}
className="mt-4 w-full rounded-lg bg-gold py-2.5 font-semibold text-black hover:brightness-110 disabled:opacity-50"
>
{busy ? "验证中…" : "进入"}
</button>
</form>
</div>
);
}