import { Clapperboard } from "lucide-react"; import { useCallback, useEffect, useState } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { clearApiKey, loadApiKey, saveApiKey } from "@/lib/apiKey"; /** * The sign-in gate. * * When the server is configured with a key, nothing is reachable until one is * supplied. The app asks for it once, keeps it in this browser, and checks it * against the server before letting the rest of the app render. */ export function LockGate({ children }: { children: React.ReactNode }) { const [state, setState] = useState<"checking" | "locked" | "unlocked">("checking"); const [key, setKey] = useState(""); const [error, setError] = useState(""); const check = useCallback(async (candidate: string) => { try { const res = await fetch("/api/v1/stats", { headers: candidate ? { "X-API-Key": candidate } : {}, }); // Only a clean 200 counts. A proxy error or an unknown route must not // read as "unlocked" and leave the app showing empty screens. return res.ok; } catch { return false; } }, []); useEffect(() => { let active = true; (async () => { try { const res = await fetch("/api/v1/health"); const health = (await res.json()) as { apiKeyRequired?: boolean }; if (!active) return; if (!health.apiKeyRequired) { setState("unlocked"); return; } const stored = loadApiKey(); if (stored && (await check(stored))) { setState("unlocked"); return; } if (stored) clearApiKey(); setState("locked"); } catch { if (active) setState("locked"); } })(); return () => { active = false; }; }, [check]); const unlock = async (event: React.FormEvent) => { event.preventDefault(); const candidate = key.trim(); if (!candidate) return; setError(""); if (!(await check(candidate))) { setError("That key was not accepted."); return; } saveApiKey(candidate); setState("unlocked"); }; if (state === "unlocked") return <>{children}; if (state === "checking") { return (
Checking access
); } return (
{/* The tally light is on: this bay is live and it is private. */}
Pitlane

Private render bay

Enter the server key to continue.

{ setKey(e.target.value); if (error) setError(""); }} placeholder="Paste the key" aria-invalid={error ? true : undefined} /> {error &&

{error}

}

The key stays in this browser and is sent only to this server.

); }