fix: Prioritize URL hash access tokens over stored tokens and clear s… #3633
+40
−6
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.
Fix: Prioritize URL access tokens over storage when using magic links
Summary
This PR fixes an issue where getAccessToken prioritizes localStorage tokens over URL hash tokens, causing magic links to fail when users already have a stored token.
Problem: When implementing passwordless authentication via magic links, the getAccessToken method checks localStorage before examining the URL hash. This means users with a stale token in storage cannot authenticate via a fresh magic link - the old token is used instead of the new one from the URL.
Solution: Modified getAccessToken to:
Check the URL hash for an access token before checking storage
Return the URL token even if a different token exists in storage
Clear (remove) the stored token when a URL token is consumed
Related Issues
Fixes #3631
Other Information
This change aligns with the expected behavior described in the original issue - if a user provides a new token in the URL, Feathers should consume it instead of using the previous one from localStorage.
Use case: Magic links to applications where a user can open a specific page without requiring login first because the token is part of the URL. Previously, opening two different magic links in the same browser would fail because the first token was cached.
Changes:
packages/authentication-client/src/core.ts
Added test case to verify URL token priority behavior