diff --git a/app/manage/page.tsx b/app/manage/page.tsx index f0d09f4..aa00e6e 100644 --- a/app/manage/page.tsx +++ b/app/manage/page.tsx @@ -97,42 +97,61 @@ const AdminPage = () => { const sessionToken = sessionStorage.getItem('admin_session_token'); const csrfToken = authState.csrfToken; - if (authStatus === 'true' && sessionToken && csrfToken) { - try { - const response = await fetch('/api/auth/validate', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'X-CSRF-Token': csrfToken - }, - body: JSON.stringify({ - sessionToken, - csrfToken - }) - }); - - if (response.ok) { - setAuthState(prev => ({ - ...prev, - isAuthenticated: true, - isLoading: false, - showLogin: false - })); - return; - } else { - sessionStorage.clear(); - } - } catch { - sessionStorage.clear(); - } + // If no session data, show login immediately + if (!authStatus || !sessionToken || !csrfToken) { + setAuthState(prev => ({ + ...prev, + isAuthenticated: false, + isLoading: false, + showLogin: true + })); + return; } - setAuthState(prev => ({ - ...prev, - isAuthenticated: false, - isLoading: false, - showLogin: true - })); + try { + const response = await fetch('/api/auth/validate', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-CSRF-Token': csrfToken + }, + body: JSON.stringify({ + sessionToken, + csrfToken + }) + }); + + if (response.ok) { + setAuthState(prev => ({ + ...prev, + isAuthenticated: true, + isLoading: false, + showLogin: false + })); + return; + } else { + // Clear invalid session + sessionStorage.removeItem('admin_authenticated'); + sessionStorage.removeItem('admin_session_token'); + setAuthState(prev => ({ + ...prev, + isAuthenticated: false, + isLoading: false, + showLogin: true + })); + } + } catch (error) { + console.error('Session validation error:', error); + // Clear session on error + sessionStorage.removeItem('admin_authenticated'); + sessionStorage.removeItem('admin_session_token'); + setAuthState(prev => ({ + ...prev, + isAuthenticated: false, + isLoading: false, + showLogin: true + })); + } }, [authState.csrfToken]); // Initialize @@ -153,7 +172,20 @@ const AdminPage = () => { if (authState.csrfToken && !authState.isLocked) { checkSession(); } - }, [authState.csrfToken, authState.isLocked, checkSession]); + }, [authState.csrfToken, authState.isLocked]); + + // Handle logout + const handleLogout = useCallback(() => { + sessionStorage.removeItem('admin_authenticated'); + sessionStorage.removeItem('admin_session_token'); + setAuthState(prev => ({ + ...prev, + isAuthenticated: false, + showLogin: true, + password: '', + error: '' + })); + }, []); // Handle login form submission const handleLogin = async (e: React.FormEvent) => { @@ -440,6 +472,17 @@ const AdminPage = () => { )} + + {/* Debug: Clear Session Button */} +