Fix: logout instantly if backend connection not established or user not found in db

This commit is contained in:
hyzen
2026-06-09 12:51:35 +05:30
parent 15266c9184
commit 3995a84a0f
47 changed files with 138 additions and 111 deletions

View File

@@ -0,0 +1,46 @@
(function () {
var path = window.location.pathname;
if (path === '/login/' || path === '/signup/') return;
// ── Save current page before navigating to login/signup ──
document.querySelectorAll('a[href="/login/"], a[href="/signup/"]').forEach(function (a) {
a.addEventListener('click', function () {
sessionStorage.setItem('f4_login_next', path);
});
});
// ── Validate session on every page load ──
if (!localStorage.getItem('f4_username')) return;
// Skip check if we just logged in (5 second grace period)
var loginTime = parseInt(localStorage.getItem('f4_login_time') || '0', 10);
if (Date.now() - loginTime < 5000) return;
fetch('https://backend.freedoms4.org/auth.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify({ action: 'check_session' }),
})
.then(function (r) {
return r.json();
})
.then(function (data) {
if (!data.valid) {
// DB gone, user deleted, or session invalid — log out immediately
localStorage.removeItem('f4_username');
localStorage.removeItem('f4_login_time');
localStorage.removeItem('f4_session_fails');
window.location.reload();
} else {
localStorage.removeItem('f4_session_fails');
}
})
.catch(function () {
// Backend completely unreachable — force logout immediately
localStorage.removeItem('f4_username');
localStorage.removeItem('f4_login_time');
localStorage.removeItem('f4_session_fails');
window.location.reload();
});
})();