Why I keep getting logged out — three causes and how to fix them

Familiar symptom: users complain the app keeps logging them out. Some every day, some whenever they close the tab, some on every device at once.
Those are three different failures with different fixes. First narrow it down, then fix it.
Two-minute triage
Three questions cut away almost everything irrelevant.
How long until it happens? A steady interval — 15 minutes, an hour, exactly one day — means an expired token, cause #1. "It varies" means read on.
Some users or everyone at once? Everyone at once is almost certainly the server: the signing key changed or the clock drifted. Individual people means it's a client-side story.
Does it reproduce in another browser? Gone in Chrome but alive in Safari — cause #2, storage cleanup. Browser-independent but dropping every device simultaneously — cause #3.
Now in order, starting with the most common.
Cause 1: the token expired and nothing renews it
An access token lives for minutes. That's by design, not a bug: it can't be revoked, so it has to die fast — details in what is a JWT.
Alongside it the server issues a refresh token, which you trade for a new pass. If that part was never written, users get dropped exactly on schedule.
How to check. Time how long until it happens. A steady 15 minutes, one hour or one day almost certainly means this. Look inside the token: there's an exp field with its death time.
How to fix. Intercept a 401 from your API, exchange the refresh token for a new pair and retry the original request. Important: refreshing must live in one place in the code, and parallel requests must queue behind it. Five simultaneous refreshes create cause #3.
Cause 2: the browser wiped the storage for you
Developers lose hours here, because on their machine everything works.
If you keep the session in localStorage or in a cookie set from JavaScript, the browser steps in. In Safari, tracking prevention deletes cookies created in JavaScript and all script-writable storage after 7 days of no interaction with the site. IndexedDB, caches and service worker registrations go with it.
The second flavour of the same problem is a cookie with no expiry date. Such a cookie counts as a session cookie and dies when the browser closes.
How to check. It reproduces in Safari but not in Chrome. Or it disappears after closing the window. Open the developer tools storage tab and look at the actual lifetimes of your entries.
How to fix. Set the cookie from the server, not from a script, with explicit flags: HttpOnly, Secure, SameSite and an expiry date. That cookie isn't subject to the seven-day sweep — and it's out of reach of foreign scripts on the page.
Cause 3: the token-theft protection fired
This one has a distinctive tell: everyone gets dropped on all devices at once, though nobody changed a password.
That's refresh token rotation at work. Every exchange issues a new pair and kills the old token. If the old one is presented again, the server has to assume a copy is loose and revoke the whole chain — that's what the OAuth security practice prescribes.
The "thief" is usually not an attacker but your own code:
- the app fired two refreshes at the same time;
- the phone was restored from a backup holding a spent token;
- the request went out, the reply died in a tunnel, and the app retried later.
How to check. In server logs look for a mass session revocation for one user and the same refresh token being presented twice.
How to fix. The same queue from cause #1, plus a small grace window: many servers accept the previous token for a few seconds, so a plain network retry doesn't take down the session.
Two small causes people forget
Clock drift. Tokens are validated against time. If the device or server clock is a couple of minutes off, the token reads as expired or not yet valid. Fix with time sync and a small tolerance on the server.
A deploy changed the signing secret. Rotate a key or spin up a new environment, and every previously issued token becomes invalid at once. The tell: all users were logged out simultaneously, exactly at release time.
FAQ
Why does it only happen on mobile and not on desktop?
Most likely it's storage. Check that the token sits in secure storage and not in a cache the system clears when space runs low.
Can I just make the token last forever?
Technically yes, practically no. Such a token can't be revoked when stolen, and one leaked log turns into permanent access to the account.
How long should a session last anyway?
For an ordinary product: a 15-minute pass and a refresh token good for a week or a month with rotation. For payments — shorter, with re-confirmation on important actions.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





