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,42 +97,61 @@ 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
try { if (!authStatus || !sessionToken || !csrfToken) {
const response = await fetch('/api/auth/validate', { setAuthState(prev => ({
method: 'POST', ...prev,
headers: { isAuthenticated: false,
'Content-Type': 'application/json', isLoading: false,
'X-CSRF-Token': csrfToken showLogin: true
}, }));
body: JSON.stringify({ return;
sessionToken,
csrfToken
})
});
if (response.ok) {
setAuthState(prev => ({
...prev,
isAuthenticated: true,
isLoading: false,
showLogin: false
}));
return;
} else {
sessionStorage.clear();
}
} catch {
sessionStorage.clear();
}
} }
setAuthState(prev => ({ try {
...prev, const response = await fetch('/api/auth/validate', {
isAuthenticated: false, method: 'POST',
isLoading: false, headers: {
showLogin: true '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]); }, [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>