"use client" import { useEffect, useState, useRef } from "react" import { useRouter } from "next/navigation" import Link from "next/link" import { useAuth } from "@/contexts/supabase-auth-context" import { Shield, Loader2, AlertCircle } from "lucide-react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Alert, AlertDescription } from "@/components/ui/alert" const ADMIN_VERIFIED_KEY = "admin_password_verified" export default function AdminAccessPage() { const { user, isLoading } = useAuth() const router = useRouter() const [checkDone, setCheckDone] = useState(false) const [passwordRequired, setPasswordRequired] = useState(false) const [password, setPassword] = useState("") const [passwordError, setPasswordError] = useState("") const [passwordLoading, setPasswordLoading] = useState(false) const passwordErrorRef = useRef(null) useEffect(() => { if (isLoading) return if (user?.user_type === "admin") { router.replace("/admin") return } if (user && (user.user_type === "user" || user.user_type === "moderator")) { setCheckDone(true) setPasswordRequired(false) return } fetch("/api/auth/verify-admin-password") .then((res) => res.json()) .then((data) => { const pwRequired = !!(data && data.adminPasswordRequired) setPasswordRequired(pwRequired) setCheckDone(true) // When no admin password is set, we show "Sign in with admin account" card instead of redirecting if (!pwRequired && user?.user_type === "admin") router.replace("/admin") }) .catch(() => { setCheckDone(true) setPasswordRequired(true) setPasswordError("Could not verify access. Please try again or go back home.") }) }, [isLoading, user, router]) useEffect(() => { if (passwordError && passwordErrorRef.current) { passwordErrorRef.current.scrollIntoView({ behavior: "smooth", block: "nearest" }) } }, [passwordError]) const handleSubmit = 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") router.push("/admin") } else { setPasswordError(data?.error || (res.status === 429 ? "Too many attempts. Please try again later." : "Invalid admin password")) } } catch { setPasswordError("Something went wrong. Please try again.") } finally { setPasswordLoading(false) } } // LOADING STATE if (isLoading || !checkDone) { return (

Checking admin access

Almost there…

) } // ACCESS DENIED - User logged in but not admin if (user && user.user_type !== "admin") { return (
Access Denied Admin privileges required
Your account type is {user.user_type}. Only users with admin privileges can access this area.

Current user: {user.user_name || user.email}

Account type: {user.user_type}

If you believe you should have admin access, please contact the system administrator.

) } // No admin password gate: show "Sign in with admin account" so user can proceed to login if (!passwordRequired) { return (
Sign in as admin Use your admin account to access the dashboard.

If your account has admin privileges, sign in and you will be taken to the admin dashboard.

) } // SHOW PASSWORD FORM return (

Sign in as admin

Enter the admin password to continue.

Tip: If you have an admin account,{" "} sign in first {" "} and you won't need the password.
setPassword(e.target.value)} className="h-10" autoFocus disabled={passwordLoading} autoComplete="current-password" aria-invalid={!!passwordError} aria-describedby={passwordError ? "admin-access-pw-error" : undefined} /> {passwordError && ( )}

Password from .env.local: ADMIN_PASSWORD

Or
) }