Keep the session on network errors when restoring it

Only a 401 clears the saved token; a network error or 5xx while the
panel restarts retries every 5 s instead of dropping to the login screen.
This commit is contained in:
2026-10-29 23:49:49 +03:00
parent 2b2ebdcfd0
commit 1ea2eda04a
+17 -5
View File
@@ -174,13 +174,25 @@ export default function App() {
// Восстановить сессию из сохранённого токена // Восстановить сессию из сохранённого токена
useEffect(() => { useEffect(() => {
// Сбрасываем сессию только на 401: сетевой сбой или 502, пока панель перезапускается,
// раньше тоже выкидывали на экран входа. В остальных случаях пробуем снова через 5 с.
const saved = localStorage.getItem(TOKEN_KEY); const saved = localStorage.getItem(TOKEN_KEY);
if (saved) { if (!saved) return;
startSession(saved).catch(() => { let timer: ReturnType<typeof setTimeout> | undefined;
localStorage.removeItem(TOKEN_KEY); let alive = true;
setToken(''); const attempt = () => {
startSession(saved).catch(e => {
if (!alive) return;
if (axios.isAxiosError(e) && e.response?.status === 401) {
localStorage.removeItem(TOKEN_KEY);
setToken('');
} else {
timer = setTimeout(attempt, 5000);
}
}); });
} };
attempt();
return () => { alive = false; clearTimeout(timer); };
}, [startSession]); }, [startSession]);
// Состояние серверов (онлайн, пиры) обновляем раз в 15 с, пока открыта панель. // Состояние серверов (онлайн, пиры) обновляем раз в 15 с, пока открыта панель.