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:
+17
-5
@@ -174,13 +174,25 @@ export default function App() {
|
||||
|
||||
// Восстановить сессию из сохранённого токена
|
||||
useEffect(() => {
|
||||
// Сбрасываем сессию только на 401: сетевой сбой или 502, пока панель перезапускается,
|
||||
// раньше тоже выкидывали на экран входа. В остальных случаях пробуем снова через 5 с.
|
||||
const saved = localStorage.getItem(TOKEN_KEY);
|
||||
if (saved) {
|
||||
startSession(saved).catch(() => {
|
||||
localStorage.removeItem(TOKEN_KEY);
|
||||
setToken('');
|
||||
if (!saved) return;
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
let alive = true;
|
||||
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]);
|
||||
|
||||
// Состояние серверов (онлайн, пиры) обновляем раз в 15 с, пока открыта панель.
|
||||
|
||||
Reference in New Issue
Block a user