From cae5d75005ce916be672ef4932504c47b7282527 Mon Sep 17 00:00:00 2001 From: bonamin Date: Mon, 28 Sep 2026 17:00:33 +0300 Subject: [PATCH] fix(waiter): don't log the waiter out when the server is unreachable at startup AuthRehydrator called logout() on ANY failure of /api/auth/me, including a network error. So opening the app while WiFi was flaky, while the server was restarting, or right after its IP changed silently threw the waiter back to the login screen and offline mode could not survive an app restart. Now a network error keeps the token and retries every 5s; a real server answer (401 and other errors) still ends the session as before. Verified on the Android emulator: cold start with the backend stopped keeps the token; starting the backend brings the app to /tables on its own. Co-Authored-By: Claude Opus 5.5 --- waiter_pwa/src/App.jsx | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/waiter_pwa/src/App.jsx b/waiter_pwa/src/App.jsx index 68b51b2..99d93a8 100644 --- a/waiter_pwa/src/App.jsx +++ b/waiter_pwa/src/App.jsx @@ -274,12 +274,31 @@ function AppLayout() { function AuthRehydrator() { const { token, user, login, logout } = useAuthStore() useEffect(() => { - if (token && !user) { - client.get('/api/auth/me') - .then(r => login(r.data, token)) - .catch(() => logout()) + if (!token || user) return + let cancelled = false + let timer = null + async function load() { + try { + const r = await client.get('/api/auth/me') + if (!cancelled) login(r.data, token) + } catch (err) { + if (cancelled) return + // Server unreachable (flaky WiFi, server restarting, IP change being + // rediscovered): keep the login and try again. Only a real answer from + // the server ends the session — 401 is also handled by the axios interceptor. + if (!err.response) { + timer = setTimeout(load, 5000) + return + } + logout() + } } - }, []) + load() + return () => { + cancelled = true + clearTimeout(timer) + } + }, [token, user]) return null }