Fix admin login verification loop
Some checks failed
CI/CD Pipeline (Using Gitea Variables & Secrets) / production (push) Failing after 10m28s
Test Gitea Variables and Secrets / test-variables (push) Successful in 2s

- Remove checkSession from useEffect dependency array to prevent infinite loop
- Improve session validation logic with better error handling
- Add clear session functionality for debugging
- Add 'Clear Session & Reload' button to help with stuck sessions
- Better session cleanup on validation errors

This should resolve the verification loop issue in the admin login.
This commit is contained in:
2025-10-16 13:30:42 +02:00
parent 1f7547a562
commit 138b473418

View File

@@ -97,7 +97,17 @@ const AdminPage = () => {
const sessionToken = sessionStorage.getItem('admin_session_token'); const sessionToken = sessionStorage.getItem('admin_session_token');
const csrfToken = authState.csrfToken; const csrfToken = authState.csrfToken;
if (authStatus === 'true' && sessionToken && csrfToken) { // If no session data, show login immediately
if (!authStatus || !sessionToken || !csrfToken) {
setAuthState(prev => ({
...prev,
isAuthenticated: false,
isLoading: false,
showLogin: true
}));
return;
}
try { try {
const response = await fetch('/api/auth/validate', { const response = await fetch('/api/auth/validate', {
method: 'POST', method: 'POST',
@@ -120,19 +130,28 @@ const AdminPage = () => {
})); }));
return; return;
} else { } else {
sessionStorage.clear(); // Clear invalid session
} sessionStorage.removeItem('admin_authenticated');
} catch { sessionStorage.removeItem('admin_session_token');
sessionStorage.clear();
}
}
setAuthState(prev => ({ setAuthState(prev => ({
...prev, ...prev,
isAuthenticated: false, isAuthenticated: false,
isLoading: false, isLoading: false,
showLogin: true 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]); }, [authState.csrfToken]);
// Initialize // Initialize
@@ -153,7 +172,20 @@ const AdminPage = () => {
if (authState.csrfToken && !authState.isLocked) { if (authState.csrfToken && !authState.isLocked) {
checkSession(); 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 // Handle login form submission
const handleLogin = async (e: React.FormEvent) => { const handleLogin = async (e: React.FormEvent) => {
@@ -440,6 +472,17 @@ const AdminPage = () => {
)} )}
</button> </button>
</form> </form>
{/* Debug: Clear Session Button */}
<div className="mt-6 pt-6 border-t border-white/20">
<button
type="button"
onClick={handleLogout}
className="w-full text-white/60 hover:text-white/80 text-sm py-2 px-4 rounded-lg border border-white/20 hover:border-white/40 transition-all"
>
Clear Session & Reload
</button>
</div>
</motion.div> </motion.div>
</div> </div>
</div> </div>