'use client'; import { useState, useEffect, type ReactNode } from 'react'; import { useAuth } from './AuthProvider'; import { AuthModal } from './AuthModal'; import { SetupModal } from './SetupModal'; interface AuthGuardProps { children: ReactNode; } interface AuthConfig { username: string | null; enabled: boolean; hasCredentials: boolean; setupCompleted: boolean; } export function AuthGuard({ children }: AuthGuardProps) { const { isAuthenticated, isLoading } = useAuth(); const [authConfig, setAuthConfig] = useState(null); const [configLoading, setConfigLoading] = useState(true); const [setupCompleted, setSetupCompleted] = useState(false); const handleSetupComplete = async () => { setSetupCompleted(true); // Refresh auth config without reloading the page await fetchAuthConfig(); }; const fetchAuthConfig = async () => { try { const response = await fetch('/api/settings/auth-credentials'); if (response.ok) { const config = await response.json() as AuthConfig; setAuthConfig(config); } } catch (error) { console.error('Error fetching auth config:', error); } finally { setConfigLoading(false); } }; useEffect(() => { void fetchAuthConfig(); }, []); // Show loading while checking auth status if (isLoading || configLoading) { return (

Loading...

); } // Show setup modal if setup has not been completed yet if (authConfig && !authConfig.setupCompleted && !setupCompleted) { return ; } // Show auth modal if auth is enabled but user is not authenticated if (authConfig && authConfig.enabled && !isAuthenticated) { return ; } // Render children if authenticated or auth is disabled return <>{children}; }