Fix useHandleCallback compatibility with React.StrictMode#10486
Merged
Conversation
djhi
suggested changes
Jan 31, 2025
Comment on lines
+34
to
+55
| handleCallbackPromise = new Promise(async (resolve, reject) => { | ||
| if (authProvider) { | ||
| if (typeof authProvider.handleCallback === 'function') { | ||
| try { | ||
| const result = | ||
| await authProvider.handleCallback({ | ||
| signal, | ||
| }); | ||
| return resolve(result ?? null); | ||
| } catch (error) { | ||
| return reject({ | ||
| redirectTo: false, | ||
| message: error.message, | ||
| }); | ||
| } | ||
| } | ||
| return resolve(); | ||
| } | ||
| return reject({ | ||
| message: 'Failed to handle login callback.', | ||
| }); | ||
| }); |
Contributor
There was a problem hiding this comment.
This changes the previous behavior. Please revert to the original code. Only keep the handleCallbackPromise creation as you did
djhi
approved these changes
Jan 31, 2025
useHandleCallback compatibility with React.StrictMode
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Many authentication providers do not accept calling their methods for validating authentication callbacks multiple times. We had the case with
ra-auth-auth0andra-keycloak(maybe evenra-supabase).Our solution until now was to implement the necessary logic in the
authProviderimplementation (storing ongoing promise in theauthProviderclosure). This is fine but it means custom implementations must do it themselves too and this is not trivial for many.Solution
Make the
useHandleCallbackresilient toStrictMode. Implement the logic that won’t callauthProvider.handleCallbackif another call is already ongoing.How To Test
Revert the fix in useHandleAuthCallback.ts and run the test "should only call handleCallback once when the component is rendered" in
useHandleAuthCallback.spec.tsx.Additional Checks
masterfor a bugfix, ornextfor a feature[ ] The PR includes one or several stories (if not possible, describe why)[ ] The documentation is up to dateScreenshot
Test without the fix: