"use client"; import { useState, useEffect, useRef } from "react"; import Link from "next/link"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { useAuth } from "@/contexts/supabase-auth-context"; import { Shield, Loader2, ArrowRight } from "lucide-react"; interface AdminGateModalProps { open: boolean; onOpenChange: (open: boolean) => void; onSuccess: () => void; } export function AdminGateModal({ open, onOpenChange, onSuccess, }: AdminGateModalProps) { const { user } = useAuth(); const [password, setPassword] = useState(""); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); const [passwordRequired, setPasswordRequired] = useState( null, ); const isAdmin = user?.user_type === "admin"; const passwordInputRef = useRef(null); // When modal opens, check if admin password is required useEffect(() => { if (!open) { setPasswordRequired(null); return; } let cancelled = false; fetch("/api/auth/verify-admin-password") .then((res) => res.json()) .then((data) => { if (!cancelled) setPasswordRequired(!!data?.adminPasswordRequired); }) .catch(() => { if (!cancelled) setPasswordRequired(false); }); return () => { cancelled = true; }; }, [open]); // Hotkey: Escape to close modal useEffect(() => { if (!open) return; const onKeyDown = (e: KeyboardEvent) => { if (e.key === "Escape") { onOpenChange(false); e.preventDefault(); } }; document.addEventListener("keydown", onKeyDown, true); return () => document.removeEventListener("keydown", onKeyDown, true); }, [open, onOpenChange]); useEffect(() => { if (open && passwordRequired === true && passwordInputRef.current) { const t = setTimeout(() => passwordInputRef.current?.focus(), 100); return () => clearTimeout(t); } }, [open, passwordRequired]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(""); setLoading(true); try { const res = await fetch("/api/auth/verify-admin-password", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ password }), }); const data = await res.json(); if (res.ok && data.ok) { if (typeof window !== "undefined") sessionStorage.setItem("admin_password_verified", "1"); setPassword(""); onOpenChange(false); onSuccess(); return; } setError(data?.error || "Invalid admin password"); } catch { setError("Something went wrong. Please try again."); } finally { setLoading(false); } }; const handleContinueWithoutPassword = () => { onOpenChange(false); onSuccess(); }; const handleClose = (open: boolean) => { if (!open) { setPassword(""); setError(""); } onOpenChange(open); }; const isLoadingGate = open && passwordRequired === null; return (
Sign in as admin Open the Admin Dashboard to manage users, builds, and activity.
{isLoadingGate && (
)} {!isLoadingGate && passwordRequired === true && (
setPassword(e.target.value)} className="h-10" autoFocus autoComplete="current-password" disabled={loading} /> {error && (

{error}

)}
Or
)} {!isLoadingGate && passwordRequired === false && ( {isAdmin ? ( ) : ( )} )}
); }