Skip to content

Conversation

@yogeshchoudhary147
Copy link
Contributor

@yogeshchoudhary147 yogeshchoudhary147 commented Dec 17, 2025

Problem

When calling getAccessTokenSilently(), the reducer creates a new user object reference even when the user data hasn't changed. This causes infinite loops in useEffect hooks that depend on the user object.

Example scenario:

const { user, getAccessTokenSilently } = useAuth0();

useEffect(() => {
  const token = await getAccessTokenSilently();
  myFunction(token, user);
}, [user, getAccessTokenSilently]); // Infinite loop!

Solution

Use deep equality comparison (isEqual from lodash-es) instead of reference equality (===) when checking if the user object has changed in the reducer.

Changes

  • Added lodash-es dependency for deep equality checking
  • Updated GET_ACCESS_TOKEN_COMPLETE and HANDLE_REDIRECT_COMPLETE cases in reducer to use isEqual
  • State is only updated when user content actually changes
  • Tree-shakeable import adds ~1.2 KB gzipped to bundle size

Testing

  • Existing tests pass
  • No breaking changes
  • Fixes infinite loop scenario

Fixes #787

@yogeshchoudhary147 yogeshchoudhary147 requested a review from a team as a code owner December 17, 2025 06:16
@yogeshchoudhary147 yogeshchoudhary147 force-pushed the fix/user-object-infinite-loop-787 branch 2 times, most recently from 2e35a6e to 049ed57 Compare December 17, 2025 06:44
Uses lodash-es isEqual for deep equality comparison to check if user
content has changed, preventing unnecessary state updates and infinite
loops in useEffect hooks.

Changes:
- Added lodash-es dependency for deep equality checking
- Updated GET_ACCESS_TOKEN_COMPLETE and HANDLE_REDIRECT_COMPLETE cases
- State only updates when user content actually changes

Fixes #787
@yogeshchoudhary147 yogeshchoudhary147 force-pushed the fix/user-object-infinite-loop-787 branch from 049ed57 to ec109c8 Compare December 17, 2025 06:45
@yogeshchoudhary147 yogeshchoudhary147 deleted the fix/user-object-infinite-loop-787 branch December 18, 2025 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Separate Token Retrieval from User Update

2 participants