"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 (