Track 07 · Lesson 12 of 18Identity and login
- 01Authentication vs authorization
- 02Browser storage compared
- 03Sessions versus tokens
- 04Inside a JWT
- 05Access and refresh tokens
- 06Where to keep tokens
- 07Choosing a design
- 08The login lifecycle
- 09The token clock
- 10Dead or just unlucky? Refresh decisions
- 11Inside a secure session cookie
- 12Five tabs, one refresh
- 13“Log in with Google”: OAuth and PKCE
- 14Two-factor login
- 15Storing passwords
- 16Logging out properly
- 17Trade-offs and common mistakes
- 18Compare the designs
Five tabs, one refresh
Lesson 12 of 18· 6 min
Users open many tabs. When a laptop wakes up, every tab finds an expired token at the same moment, and each fires several requests. If refresh tokens rotate, two refreshes racing with the same token can kill each other, and log the user out for having tabs open.
Wake the laptop
Then switch the tools off one by one
- One shared promise per tab: 3 requests, 1 refresh.
- Only one tab refreshes at a time.
- Inside the lock: if the expiry changed, someone already refreshed. Skip.
- The tab that refreshed pushes the new token to all others.
Tab 13 req
refresh → serverTab 23 req
skippedTab 33 req
skippedTab 43 req
skippedTab 53 req
skipped1refresh call hit the server
Ideal. One refresh reaches the server; every other tab and request reuses its token. This is the recommended setup.
Four tools, four jobs#
- Single-flight (per tab): one shared promise, so three requests in one tab cause one refresh.
- Web Locks (across tabs):
navigator.lockslets only one tab refresh at a time. Each tab is its own JavaScript world, so per-tab locks don’t help here. - Expiry double-check: inside the lock, check again. If the token’s expiry changed, someone already refreshed: skip the network.
- BroadcastChannel: the tab that refreshed pushes the new token to all the others.
Classic cookie sessions don’t have this problem; every token-based setup does.
Check yourself