FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: Prioritize URL hash access tokens over stored tokens and clear s… by faizi-7 · Pull Request #3633 · feathersjs/feathers · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .ts  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
20 changes: 14 additions & 6 deletions packages/authentication-client/src/core.ts
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
Expand Up @@ -114,16 +114,24 @@ export class AuthenticationClient {
}

/**
* Returns the access token from storage or the window location hash.
* Returns the access token from the window location hash or storage.
*
* @returns The access token from storage or location hash
* @returns The access token from location hash or storage
*/
getAccessToken(): Promise<string | null> {
return this.storage.getItem(this.options.storageKey).then((accessToken: string) => {
if (!accessToken && typeof window !== 'undefined' && window.location) {
return this.getFromLocation(window.location)
}
if (typeof window !== 'undefined' && window.location) {
return this.getFromLocation(window.location).then((urlToken) => {
if (urlToken) {
return this.removeAccessToken().then(() => urlToken)
}

return this.storage.getItem(this.options.storageKey).then((storageToken: string) => {
return storageToken || null
})
})
}

return this.storage.getItem(this.options.storageKey).then((accessToken: string) => {
return accessToken || null
})
}
Expand Down
26 changes: 26 additions & 0 deletions packages/authentication-client/test/index.test.ts
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
Expand Up @@ -96,6 +96,32 @@ describe('@feathersjs/authentication-client', () => {
}
})

it('getAccessToken prefers URL token over storage token (#3631)', async () => {
const auth = app.authentication
const storageToken = 'old-storage-token'
const urlToken = 'new-url-token'

await auth.setAccessToken(storageToken)

let stored = await auth.storage.getItem(auth.options.storageKey)
assert.strictEqual(stored, storageToken)

const originalWindow = (global as any).window
; (global as any).window = {
location: { hash: `access_token=${urlToken}` }
}

try {
const token = await auth.getAccessToken()
assert.strictEqual(token, urlToken, 'Should return URL token over storage token')

stored = await auth.storage.getItem(auth.options.storageKey)
assert.strictEqual(stored, undefined, 'Storage should be cleared when URL token is used')
} finally {
; (global as any).window = originalWindow
}
})

it('authenticate, authentication hook, login event', async () => {
const data = {
strategy: 'testing'
Expand Down

Back | FazBrowse Home | New Git URL