Fix useAuthState may logout even though logoutOfFailure is false #10280
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
Calling
useAuthState()
should not logout the user is anonymous. Yet, in some situations, it does.In particular, calling
useAuthState
in a CRUD page (e.g.<Edit disableAuthentication>
) logs the user out.Analysis
In this case, there are two calls to
authProvider.checkAuth
with the same params (and the same query key):useCheckAuth()
withlogoutOnFailure = false
. The auth provider returns an error, which should not be used to logout<Edit disableAuthentication>
, which should not execute theauthProvider.checkAuth
as thedisableAuthentication
prop leads to adisabled
queryYet, as the two calls use the same query key, react-query populates the response of the second call with the response from the first one. There IS an error in the second call even though the query fn was never called. And since
logoutOnFailure
is true for the second call, this logs the user out.Solution
In the
useEffect
that monitor the success and error state, do not trigger effects if the query is disabled.How To Test
Test with the following example: https://github.com/p4it-kft/react-admin-test
log out, then go to a user edit page directlyu by typing /users/1 in the URL
Additional Checks
master
for a bugfix, ornext
for a feature[ ] The PR includes unit tests (if not possible, describe why)[ ] The PR includes one or several stories (if not possible, describe why)[ ] The documentation is up to date