"use client"; import { useAuth } from "@/contexts/supabase-auth-context"; import { useRouter, usePathname } from "next/navigation"; import { useEffect, useState, useRef } from "react"; import { Shield, Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; const ADMIN_VERIFIED_KEY = "admin_password_verified"; export default function AdminLayout({ children, }: { children: React.ReactNode; }) { const { user, isLoading } = useAuth(); const router = useRouter(); const pathname = usePathname(); const [checkDone, setCheckDone] = useState(false); const [passwordRequired, setPasswordRequired] = useState(false); const [showPasswordForm, setShowPasswordForm] = useState(false); const [password, setPassword] = useState(""); const [passwordError, setPasswordError] = useState(""); const [passwordLoading, setPasswordLoading] = useState(false); const passwordErrorRef = useRef(null); // Check: admin password required? If yes, no login needed—just password. If no, require logged-in admin. useEffect(() => { if (isLoading) return; const goLogin = () => { router.replace(`/login?redirect=${encodeURIComponent(pathname || "/admin")}`); }; const goDashboard = () => { router.replace("/dashboard"); }; fetch("/api/auth/verify-admin-password") .then((res) => res.json()) .then((data) => { const needPassword = !!data?.adminPasswordRequired; setPasswordRequired(needPassword); const verified = typeof window !== "undefined" && sessionStorage.getItem(ADMIN_VERIFIED_KEY) === "1"; if (needPassword) { if (verified) { setShowPasswordForm(false); } else { setShowPasswordForm(true); } setCheckDone(true); } else { if (!user) { goLogin(); return; } if (user.user_type !== "admin") { goDashboard(); return; } setCheckDone(true); } }) .catch(() => { if (user?.user_type === "admin") { setCheckDone(true); setShowPasswordForm(false); } else { goDashboard(); } }); }, [user, isLoading, router, pathname]); const handlePasswordSubmit = async (e: React.FormEvent) => { e.preventDefault(); setPasswordError(""); setPasswordLoading(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_VERIFIED_KEY, "1"); setShowPasswordForm(false); setPassword(""); } else { setPasswordError(data?.error || "Invalid admin password"); } } catch { setPasswordError("Something went wrong. Please try again."); } finally { setPasswordLoading(false); } }; // Scroll error into view when password error is set useEffect(() => { if (passwordError && passwordErrorRef.current) { passwordErrorRef.current.scrollIntoView({ behavior: "smooth", block: "nearest" }); } }, [passwordError]); if (isLoading || !checkDone) { return (

Checking admin access

Almost there…

); } // Password required but not verified: show password form (no login required) if (passwordRequired && showPasswordForm) { return (

Admin Dashboard

Enter the admin password to continue.

setPassword(e.target.value)} className="h-10" autoFocus disabled={passwordLoading} autoComplete="current-password" aria-invalid={!!passwordError} aria-describedby={passwordError ? "admin-pw-error" : undefined} /> {passwordError && ( )}
); } return <>{children}; }