"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 (
载入中…
); return (
⚽

世界杯 预测引擎

本站仅供小范围使用,请输入访问密码

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 &&
{err}
}
); }