Skip to content

Commit b9feb34

Browse files
fix: prevent unnecessary user object updates causing infinite loops
This fixes an issue where getAccessTokenSilently would create a new user object reference even when the user data hadn't changed, causing useEffect hooks with user dependencies to re-run infinitely. The reducer now uses deep equality comparison (lodash.isequal) instead of reference equality when checking if the user object has changed. This prevents unnecessary state updates and re-renders. Changes: - Added lodash.isequal dependency for deep equality comparison - Updated GET_ACCESS_TOKEN_COMPLETE and HANDLE_REDIRECT_COMPLETE cases to use isEqual - State is only updated when user content actually changes Fixes #787
1 parent f244784 commit b9feb34

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

package-lock.json

Lines changed: 27 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"@testing-library/jest-dom": "6.8.0",
5959
"@testing-library/react": "16.3.0",
6060
"@types/jest": "^29.5.14",
61+
"@types/lodash.isequal": "^4.5.8",
6162
"@types/react": "19.1.8",
6263
"@types/react-dom": "19.1.6",
6364
"@typescript-eslint/eslint-plugin": "^8.36.0",
@@ -95,6 +96,7 @@
9596
"react-dom": "^16.11.0 || ^17 || ^18 || ~19.0.1 || ~19.1.2 || ^19.2.1"
9697
},
9798
"dependencies": {
98-
"@auth0/auth0-spa-js": "^2.11.0"
99+
"@auth0/auth0-spa-js": "^2.11.0",
100+
"lodash.isequal": "^4.5.0"
99101
}
100102
}

src/reducer.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { User } from '@auth0/auth0-spa-js';
22
import { AuthState } from './auth-state';
3+
import isEqual from 'lodash.isequal';
34

45
type Action =
56
| { type: 'LOGIN_POPUP_STARTED' }
@@ -35,7 +36,7 @@ export const reducer = <TUser extends User = User>(state: AuthState<TUser>, acti
3536
};
3637
case 'HANDLE_REDIRECT_COMPLETE':
3738
case 'GET_ACCESS_TOKEN_COMPLETE':
38-
if (state.user === action.user) {
39+
if (isEqual(state.user, action.user)) {
3940
return state;
4041
}
4142
return {

0 commit comments

Comments
 (0)