Skip to content
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

Make sure handleRedirectCallback is only called once in StrictMode React 18 #355

Merged
merged 2 commits into from
Apr 28, 2022

Conversation

adamjmcgrath
Copy link
Contributor

@adamjmcgrath adamjmcgrath commented Apr 28, 2022

Description

handleRedirectCallback can only be called once, multiple invocations will result in "Invalid State" errors since the first invocation has the side effect of clearing the transaction state (SessionStorage or cookies) causing the second invocation to fail.

Because of this side effect handleRedirectCallback doesn't play well with the new Reusable State checks in StrictMode.

When StrictMode remounts the Auth0Provider, the user hasn't logged in yet, handleRedirectCallback fails and the SDK initialises as logged out before the first handleRedirectCallback completes and initialises the SDK as logged in.

One symptom of this is that If you have code that detects if the user is logged out and redirects them to login (like withAuthenticationRequired does) you can get in an infinite loop as even successful logins will briefly report failures.

References

The recommended solution for code that can only be run once is to use a "ref"

image

reactwg/react-18/discussions/18

Fixes #343

Testing

Added test per reactwg/react-18/discussions/17

Checklist

  • I have added documentation for new/changed functionality in this PR or in auth0.com/docs
  • All active GitHub checks for tests, formatting, and security are passing
  • The correct base branch is being used, if not master

@adamjmcgrath adamjmcgrath requested a review from a team as a code owner April 28, 2022 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Race condition between isAuthenticated and isLoading (redirects) (react-router-v6)
2 participants