'use client'; import { useState, useEffect } from 'react'; interface AuthGateProps { children: React.ReactNode; } const SESSION_KEY = 'auth_session'; export default function AuthGate({ children }: AuthGateProps) { const [isAuthenticated, setIsAuthenticated] = useState(false); const [isChecking, setIsChecking] = useState(true); const [password, setPassword] = useState(''); const [error, setError] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); // Check for existing session on mount useEffect(() => { const session = sessionStorage.getItem(SESSION_KEY); if (session === 'authenticated') { setIsAuthenticated(true); } setIsChecking(false); }, []); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setError(''); setIsSubmitting(true); try { const basePath = process.env.NEXT_PUBLIC_BASE_PATH || ''; const response = await fetch(`${basePath}/api/auth`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ password }) }); const data = await response.json(); if (response.ok && data.authenticated) { sessionStorage.setItem(SESSION_KEY, 'authenticated'); setIsAuthenticated(true); setPassword(''); } else { setError('Invalid password'); } } catch (err) { setError('Authentication failed'); } finally { setIsSubmitting(false); } } if (isChecking) { return (
Loading...
); } if (!isAuthenticated) { return (

Authentication Required

setPassword(e.target.value)} className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none" placeholder="Enter password" disabled={isSubmitting} autoFocus />
{error && (
{error}
)}
); } return <>{children}; }