fix: проверка валидности токена при загрузке страницы

Раньше: если token есть в localStorage — сразу showChat().
Теперь: fetch /api/history → если 401 → tryRefresh → если fail → logout.
Предотвращает "вечные сессии" с просроченным токеном.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
anor
2026-03-30 04:51:36 +10:00
co-authored by Claude Opus 4.6
parent 56ba1bd5aa
commit a514209ea8
+11 -1
View File
@@ -180,7 +180,17 @@ function checkAdmin() {
}
// --- Init ---
if (token) { checkAdmin(); showChat(); } else showLogin();
// Validate token on page load (don't trust localStorage blindly)
(async () => {
if (!token) { showLogin(); return; }
try {
const r = await fetch(API + '/api/history?limit=1', {headers: {'Authorization': 'Bearer ' + token}});
if (r.ok) { checkAdmin(); showChat(); return; }
} catch {}
// Access token expired — try refresh
if (await tryRefreshToken()) { checkAdmin(); showChat(); }
else { logout(); showLogin(); }
})();
function showLogin() {
document.getElementById('login-screen').style.display = 'flex';