feat(auth): single-flight token refresh with cross-tab coordination - #20
Open
armorbreak001 wants to merge 2 commits into
Open
armorbreak001 wants to merge 2 commits into
armorbreak001 wants to merge 2 commits into
Conversation
Fixes the token-refresh race condition where concurrent callers
(multiple tabs/components) each issued independent refresh requests,
risking stale token writes and inconsistent session state.
- Shared in-flight promise: concurrent refresh callers await the
same operation and resolve from one final token state
- Failure clears the in-flight marker so a later refresh retries
- BroadcastChannel notifications ('tot_auth_refresh') propagate
success/failure across tabs; other tabs reload tokens from
storage on success and clear state on failure; falls back to
silent no-op where BroadcastChannel is unavailable; no raw
tokens are logged or broadcast
- Logout cancels any in-flight refresh
- Documented behavior in module header + inline comments
- 8 focused tests (node:test) covering concurrent success,
failure retry, sequential refreshes, and broadcasts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the token-refresh race condition in
frontend/src/services/auth.tswhere concurrent refresh callers (multiple tabs/components) each issued independent refresh requests. Implements single-flight coordination so all concurrent callers share one in-flight refresh operation, with safe cross-tab notification via BroadcastChannel and proper cleanup on both success and failure.Closes #1
Changes
frontend/src/services/auth.tsinflightRefreshshared promise: concurrentrefreshTokens()callers await the same operation and resolve from the same final token statefinally) so a later refresh can retry normallybroadcastRefreshResult()— postsrefresh-success/refresh-failedto atot_auth_refreshBroadcastChannel; silently no-ops where BroadcastChannel is unavailable; no raw tokens are logged or broadcastsetupCrossTabListener()— onrefresh-successother tabs reload tokens from storage and reschedule refresh; onrefresh-failedthey clear local session statelogout()now cancels any in-flight refresh so pending refreshes cannot resurrect statefrontend/src/__tests__/single-flight-refresh.test.js(new)Testing
node --test src/__tests__/single-flight-refresh.test.js→ 8/8 pass (~170ms)python3 build.py→ completed; diagnostic committed asbuild-94e0fb08(.json+ 7.logdchunks, reassembly command in build output; chunk password:52e21717e4d8555f95ab)Checklist
diagnostic/build-94e0fb08*)