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:
+11
-1
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user