fix(clerk-js): Handle missing window.location in React Native navigation #7654
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.
Description
The fix that I have added is for the "needs second factor":
When signing in with password on a new/unrecognized device, Clerk's Client Trust feature returns needs_second_factor status. Clerk then tries to navigate to the second factor UI by calling its internal navigate() function.
The problem: navigate() in packages/clerk-js/src/core/clerk.ts does this:
let toURL = new URL(to, window.location.href);
In React Native:
Result: Crash with "Cannot read property 'href' of undefined"
We now check for the window location, and if it cant be found, we will replace the navigation with a router.push / router.replace because in React Native, there's no window.location, the router is the only way to navigate. The fix checks: if window.location is undefined, use the custom router. If no custom router was provided either, warn and bail out gracefully instead of crashing.
MOBILE-392: Evan Bacon - Expo SDK issues
Checklist
pnpm testruns as expected.pnpm buildruns as expected.Type of change