-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix external auth buttons in React Strict Mode #6175
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "saleor-dashboard": patch | ||
| --- | ||
|
|
||
| Fix race condition in React Strict Mode when fetching external authenticators |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import { useAvailableExternalAuthenticationsLazyQuery } from "@dashboard/graphql"; | ||
| import { useAvailableExternalAuthenticationsQuery } from "@dashboard/graphql"; | ||
| import useNavigator from "@dashboard/hooks/useNavigator"; | ||
| import { getAppMountUriForRedirect } from "@dashboard/utils/urls"; | ||
| import { useEffect } from "react"; | ||
| import { useCallback, useEffect, useRef } from "react"; | ||
| import urlJoin from "url-join"; | ||
| import useRouter from "use-react-router"; | ||
|
|
||
|
|
@@ -21,18 +21,26 @@ const LoginView = ({ params }: LoginViewProps) => { | |
| const { location } = useRouter(); | ||
| const { login, requestLoginByExternalPlugin, loginByExternalPlugin, authenticating, errors } = | ||
| useUser(); | ||
| const [ | ||
| queryExternalAuthentications, | ||
| { data: externalAuthentications, loading: externalAuthenticationsLoading }, | ||
| ] = useAvailableExternalAuthenticationsLazyQuery(); | ||
| const { | ||
| fallbackUri, | ||
| requestedExternalPluginId, | ||
| isCallbackPath, | ||
| setFallbackUri, | ||
| setRequestedExternalPluginId, | ||
| } = useAuthParameters(); | ||
|
|
||
| // Determine if we should skip the query (when we're on callback path with auth params) | ||
| const { code, state } = params; | ||
| const isExternalAuthCallback = !!(code && state && isCallbackPath); | ||
|
|
||
| const { data: externalAuthentications, loading: externalAuthenticationsLoading } = | ||
| useAvailableExternalAuthenticationsQuery({ | ||
| skip: isExternalAuthCallback, | ||
| }); | ||
|
|
||
| const { lastLoginMethod, setLastLoginMethod } = useLastLoginMethod(); | ||
| // Track if external auth callback has been processed to avoid double-processing in Strict Mode | ||
| const externalAuthProcessedRef = useRef(false); | ||
|
|
||
mirekm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const handleSubmit = async (data: LoginFormData) => { | ||
| if (!login) { | ||
|
|
@@ -61,38 +69,49 @@ const LoginView = ({ params }: LoginViewProps) => { | |
| window.location.href = data.authorizationUrl; | ||
| } | ||
| }; | ||
| const handleExternalAuthentication = async (code: string, state: string) => { | ||
| await loginByExternalPlugin!(requestedExternalPluginId, { | ||
| code, | ||
| state, | ||
| }); | ||
| setRequestedExternalPluginId(null); | ||
| navigate(fallbackUri); | ||
| setFallbackUri(null); | ||
| }; | ||
| const handleExternalAuthentication = useCallback( | ||
| async (code: string, state: string) => { | ||
| await loginByExternalPlugin!(requestedExternalPluginId, { | ||
| code, | ||
| state, | ||
| }); | ||
| setRequestedExternalPluginId(null); | ||
| navigate(fallbackUri); | ||
| setFallbackUri(null); | ||
| }, | ||
| [ | ||
| loginByExternalPlugin, | ||
| requestedExternalPluginId, | ||
| navigate, | ||
| fallbackUri, | ||
| setRequestedExternalPluginId, | ||
| setFallbackUri, | ||
| ], | ||
| ); | ||
|
|
||
| // Reset the ref when we're no longer on a callback path, allowing new auth attempts | ||
| useEffect(() => { | ||
| const { code, state } = params; | ||
| const externalAuthParamsExist = code && state && isCallbackPath; | ||
|
|
||
| if (!externalAuthParamsExist) { | ||
| queryExternalAuthentications(); | ||
| if (!isExternalAuthCallback) { | ||
| externalAuthProcessedRef.current = false; | ||
| } | ||
| }, [isCallbackPath, params, queryExternalAuthentications]); | ||
| }, [isExternalAuthCallback]); | ||
|
|
||
| useEffect(() => { | ||
| const { code, state } = params; | ||
| const externalAuthParamsExist = code && state && isCallbackPath; | ||
| const externalAuthNotPerformed = !authenticating && !errors.length; | ||
|
|
||
| if (externalAuthParamsExist && externalAuthNotPerformed) { | ||
| handleExternalAuthentication(code, state); | ||
| // Guard against double-processing in React Strict Mode | ||
| if (isExternalAuthCallback && externalAuthNotPerformed && !externalAuthProcessedRef.current) { | ||
| externalAuthProcessedRef.current = true; | ||
| handleExternalAuthentication(code!, state!); | ||
| } | ||
|
Comment on lines
+102
to
106
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: again we probably have issue somewhere else, we shouldn't write specific fixes to Strict mode |
||
|
|
||
| return () => { | ||
| setRequestedExternalPluginId(null); | ||
| setFallbackUri(null); | ||
| }; | ||
| }, []); | ||
| }, [ | ||
| isExternalAuthCallback, | ||
| authenticating, | ||
| errors.length, | ||
| handleExternalAuthentication, | ||
| code, | ||
| state, | ||
| ]); | ||
|
|
||
| return ( | ||
| <LoginPage | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: I don't think we should create hacks to work around strict mode, if we have issue there it probably means we have something wrong with our implementation. It's worth checking ESLint warnings.
There's also a possibility we could introduce a bug here, as copilot wrote: https://github.com/saleor/saleor-dashboard/pull/6175/files#r2589250212
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I see it it's not necessarily a hack for React Strict Mode per se. What should we do when component remounts and Apollo has the lazy query in flight?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assuming we can't have that race condition realistically outside of strict mode the ref is indeed unnecessary but what do we do then for the strict mode so it works as expected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment was I think already addressed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think apollo should cancel query if it is in flight and component re-renders, if it doesn't then probably there's some bug in Apollo, it might be because we use an old version though :/