Two tabs, one refresh token
A logout bug that most test suites never see, because they open only 1 tab.
In early 2025 I reviewed a fix on a site I work on. The location lookup and the postal-code search had started failing with a 401. The cause was the access token. When it expired, more than one request tried to renew it at the same time.
The browser has a worse version of the same race. Say a user has your app open in 2 tabs and the access token expires. Both tabs get a 401, and both send the same refresh token to get a new one. The provider thinks the second request came from a thief, so it logs the user out in every tab. And the user has no idea why.
You can watch it happen here. Press the button. Then pick each of the other 2 options and press the button again.
What does the provider see when 2 tabs refresh at once?
Simulated tabs and providerTab 1
Signed in
holds rt_1
Tab 2
Signed in
holds rt_1
Shared storage has the refresh token rt_1
What the provider receives
- No refresh requests yet
rt_1 and rt_2 are refresh tokens. The provider retires a token when it issues the next one. A crossed-out token no longer works. Both tabs and the provider run inside this page, and nothing is sent anywhere.This only happens when the sign-in provider rotates refresh tokens. With rotation, every refresh gives you a new refresh token too, and the old one is retired. If a retired token ever comes back, the provider assumes someone stole it and revokes every token from that login. But it cannot know the second request was only your other tab.
Let 1 tab refresh at a time
Some providers let you fix this on their side. Auth0, for example, has a “Rotation Overlap Period” that keeps accepting the old token for a few seconds. But then a stolen token also works for those few seconds, and I’d rather not open that window.
I fix it on the client instead, because then the provider’s reuse detection stays strict. Only 1 refresh runs and every other caller waits for its result. People call this single-flight. And the Web Locks API makes it easy, because it gives you a named lock that every tab on the same origin shares.
function fresh(t) {
const now = Date.now() / 1000;
return t.exp - now > 30;
}
async function refreshOnce() {
// Another tab may have refreshed
// while we waited, so read again.
const t = readTokens();
if (fresh(t)) return t.access;
// Only 1 tab at a time gets here.
const next = await refresh(t);
writeTokens(next);
return next.access;
}
function accessToken() {
// No lock while the token is good.
const t = readTokens();
if (fresh(t)) return t.access;
return navigator.locks
.request("auth", refreshOnce);
}
readTokens and writeTokens use storage that all tabs share, such as localStorage. So when the second tab finally gets the lock, it reads storage again, finds the fresh token and never calls the provider. That second read is the important part. It is the only difference between “A lock” and “A lock and a second read” in the figure. Without it the lock only makes the 2 refreshes run one after the other, and the provider still sees the old token twice.
This code assumes the tokens sit where JavaScript can read them. With HttpOnly cookies the lock works the same way, but the second read needs something else to check, such as an expiry time kept in shared storage.
You do not even need 2 tabs for this race. When 5 API calls in 1 tab fail with a 401 at the same moment, all 5 try to refresh. So put this guard on any refresh-on-401 handler that more than one caller can hit at once.
The lock does not cover everything. If the provider rotates the token and the response gets lost on the way back, the tab retries with a retired token and the user is logged out anyway. Okta’s docs give this as the reason for its grace period, that on a poor network new tokens “might not reach the client app.”