Add optional callback parameter to authorize(), fired before the redirect occurs. #1438
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.
I would have made an issue for this, but the code change is so small that I think it's harmless to leave it as a PR. I made the change as short as possible without disrupting existing convention (though my use of
?.()
is the first in the repo).Changes
Adds an optional callback field to the public API of
WebAuth.authorize
such that developers can call some extra JavaScript before the user is redirected to the Auth0 login flow. i.e., the signature ofauthorize
is now:This solves an extremely specific issue that I encountered using a stateful SPA with the Auth0 JS SDK. For example, consider a React app wants to log in the user, display some loading state or play some animation before that happens, but set the page state to not display that animation just before the user is redirected.
While this risks some amount of content flash, it prevents certain browsers from hanging onto the incorrect state. The issue I was encountering, in particular, is that when the user presses their browser's back button, the browser held onto the last state, and would incorrectly keep said animation playing, or the loading state enabled.
I have tried using a few other methods to get the page state to "reset" before redirection, e.g. https://github.com/streamich/react-use/blob/master/docs/useUnmount.md or
onbeforeunload
, but the React method is unpredictable, and running JavaScript before redirection is fairly unpredictable across browsers.Perhaps there's an easier way of solving this problem that I missed? Any input is appreciated.
References
N/A
Testing
Checklist