Skip to content

Commit

Permalink
fix: multiplied code exchange requests in strict mode (#1012)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ignatella committed Jun 20, 2023
1 parent 1ab8e3c commit 2118bb2
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,14 @@ export const AuthProvider: FC<PropsWithChildren<AuthProviderProps>> = ({
* Handles user auth flow on initial render.
*/
useEffect(() => {
let isMounted = true;
isMountedRef.current = true;
setIsLoading(true);
(async () => {
const user = await userManager!.getUser();
if (!user || user.expired) {
// isMountedRef cannot be used here as its value is updated by next useEffect.
// We intend to keep context of current useEffect.
if (isMounted && (!user || user.expired)) {
// If the user is returning back from the OIDC provider, get and set the user data.
if (hasCodeInUrl(location)) {
const user = (await userManager.signinCallback()) || null;
Expand All @@ -146,6 +149,7 @@ export const AuthProvider: FC<PropsWithChildren<AuthProviderProps>> = ({
setIsLoading(false);
})();
return () => {
isMounted = false;
isMountedRef.current = false;
};
}, [location, userManager, autoSignIn, onBeforeSignIn, onSignIn]);
Expand Down

0 comments on commit 2118bb2

Please sign in to comment.