-
Notifications
You must be signed in to change notification settings - Fork 58
fix(auth): coordinate token refresh across pages #3377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+214
−45
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e31a332
fix(auth): coordinate token refresh across pages
nighca 18f0130
refactor(auth): centralize user state persistence
nighca 22e308a
refactor(auth): rely on web locks for refresh coordination
nighca 140fb08
refactor(auth): serialize access token reads
nighca 9e6d562
refactor(auth): clarify refresh token snapshot
nighca 872b3b0
refactor(auth): clarify shared session recovery
nighca 941cf40
refactor(auth): simplify refresh failure handling
nighca 41d2ed1
refactor(auth): await refreshed token response
nighca d6a808f
perf(auth): skip lock for valid access tokens
nighca 11da4af
fix(auth): clear state when shared storage is missing
nighca 3c2b05d
refactor(auth): synchronize user state through storage
nighca acb7f36
docs(auth): explain cross-tab token refresh lock
nighca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const userStateStorageKey = 'builder-user' | ||
|
|
||
| describe('ensureAccessToken', () => { | ||
| let ensureAccessToken: typeof import('./signed-in').ensureAccessToken | ||
| let initUserState: typeof import('./signed-in').initUserState | ||
| let accountOAuthApisForXBuilder: typeof import('@/apis/account/oauth').accountOAuthApisForXBuilder | ||
|
|
||
| beforeEach(async () => { | ||
| vi.resetModules() | ||
| localStorage.clear() | ||
| ;({ ensureAccessToken, initUserState } = await import('./signed-in')) | ||
| ;({ accountOAuthApisForXBuilder } = await import('@/apis/account/oauth')) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks() | ||
| }) | ||
|
|
||
| it('uses credentials refreshed by another page while waiting for the refresh lock', async () => { | ||
| localStorage.setItem( | ||
| userStateStorageKey, | ||
| JSON.stringify({ | ||
| accessToken: 'expired-access-token', | ||
| accessTokenExpiresAt: Date.now(), | ||
| refreshToken: 'old-refresh-token', | ||
| username: 'alice' | ||
| }) | ||
| ) | ||
| const request = vi.fn(async (_name: string, callback: () => Promise<void>) => { | ||
| localStorage.setItem( | ||
| userStateStorageKey, | ||
| JSON.stringify({ | ||
| accessToken: 'new-access-token', | ||
| accessTokenExpiresAt: Date.now() + 60 * 60 * 1000, | ||
| refreshToken: 'new-refresh-token', | ||
| username: 'alice' | ||
| }) | ||
| ) | ||
| await callback() | ||
| }) | ||
| Object.defineProperty(navigator, 'locks', { | ||
| configurable: true, | ||
| value: { | ||
| request | ||
| } | ||
| }) | ||
| const refreshToken = vi.spyOn(accountOAuthApisForXBuilder, 'refreshToken') | ||
|
|
||
| initUserState('client-id') | ||
|
|
||
| await expect(ensureAccessToken()).resolves.toBe('new-access-token') | ||
| expect(request).toHaveBeenCalledWith('builder-user-access-token', expect.any(Function)) | ||
| expect(refreshToken).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('returns the cached access token without acquiring the lock', async () => { | ||
| localStorage.setItem( | ||
| userStateStorageKey, | ||
| JSON.stringify({ | ||
| accessToken: 'access-token', | ||
| accessTokenExpiresAt: Date.now() + 60 * 60 * 1000, | ||
| refreshToken: 'refresh-token', | ||
| username: 'alice' | ||
| }) | ||
| ) | ||
| const request = vi.fn(async (_name: string, callback: () => Promise<void>) => callback()) | ||
| Object.defineProperty(navigator, 'locks', { | ||
| configurable: true, | ||
| value: { | ||
| request | ||
| } | ||
| }) | ||
|
|
||
| initUserState('client-id') | ||
|
|
||
| await expect(ensureAccessToken()).resolves.toBe('access-token') | ||
| expect(request).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('clears cached state when the shared state is removed', async () => { | ||
| localStorage.setItem( | ||
| userStateStorageKey, | ||
| JSON.stringify({ | ||
| accessToken: 'access-token', | ||
| accessTokenExpiresAt: Date.now() + 60 * 60 * 1000, | ||
| refreshToken: 'refresh-token', | ||
| username: 'alice' | ||
| }) | ||
| ) | ||
| Object.defineProperty(navigator, 'locks', { | ||
| configurable: true, | ||
| value: { | ||
| request: vi.fn(async (_name: string, callback: () => Promise<void>) => callback()) | ||
| } | ||
| }) | ||
| initUserState('client-id') | ||
|
|
||
| localStorage.removeItem(userStateStorageKey) | ||
| window.dispatchEvent(new StorageEvent('storage', { key: userStateStorageKey })) | ||
|
|
||
| await expect(ensureAccessToken()).resolves.toBe(null) | ||
| }) | ||
|
|
||
| it('clears cached state when the shared storage is cleared', async () => { | ||
| localStorage.setItem( | ||
| userStateStorageKey, | ||
| JSON.stringify({ | ||
| accessToken: 'access-token', | ||
| accessTokenExpiresAt: Date.now() + 60 * 60 * 1000, | ||
| refreshToken: 'refresh-token', | ||
| username: 'alice' | ||
| }) | ||
| ) | ||
| Object.defineProperty(navigator, 'locks', { | ||
| configurable: true, | ||
| value: { | ||
| request: vi.fn(async (_name: string, callback: () => Promise<void>) => callback()) | ||
| } | ||
| }) | ||
| initUserState('client-id') | ||
|
|
||
| localStorage.clear() | ||
| window.dispatchEvent(new StorageEvent('storage', { key: null })) | ||
|
|
||
| await expect(ensureAccessToken()).resolves.toBe(null) | ||
| }) | ||
|
|
||
| it('does not write state back after receiving a storage event', () => { | ||
| initUserState('client-id') | ||
| localStorage.setItem( | ||
| userStateStorageKey, | ||
| JSON.stringify({ | ||
| accessToken: 'access-token', | ||
| accessTokenExpiresAt: Date.now() + 60 * 60 * 1000, | ||
| refreshToken: 'refresh-token', | ||
| username: 'alice' | ||
| }) | ||
| ) | ||
| const setItem = vi.spyOn(Storage.prototype, 'setItem') | ||
|
|
||
| window.dispatchEvent(new StorageEvent('storage', { key: userStateStorageKey })) | ||
|
|
||
| expect(setItem).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.