Skip to content
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

backport memoize context value in auth provider #1379

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 23 additions & 28 deletions src/AuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import React, {
useCallback,
useEffect,
useMemo,
useReducer,
useRef,
useState,
} from "react";
import React from "react";
import { UserManager, type UserManagerSettings, User } from "oidc-client-ts";
import type {
SignoutRedirectArgs,
Expand Down Expand Up @@ -156,15 +149,15 @@ export const AuthProvider = (props: AuthProviderProps): JSX.Element => {
...userManagerSettings
} = props;

const [userManager] = useState(() => {
const [userManager] = React.useState(() => {
return userManagerProp ??
(UserManagerImpl
? new UserManagerImpl(userManagerSettings as UserManagerSettings)
: ({ settings: userManagerSettings } as UserManager));
});

const [state, dispatch] = useReducer(reducer, initialAuthState);
const userManagerContext = useMemo(
const [state, dispatch] = React.useReducer(reducer, initialAuthState);
const userManagerContext = React.useMemo(
() =>
Object.assign(
{
Expand Down Expand Up @@ -202,9 +195,9 @@ export const AuthProvider = (props: AuthProviderProps): JSX.Element => {
),
[userManager],
);
const didInitialize = useRef(false);
const didInitialize = React.useRef(false);

useEffect(() => {
React.useEffect(() => {
if (!userManager || didInitialize.current) {
return;
}
Expand All @@ -227,7 +220,7 @@ export const AuthProvider = (props: AuthProviderProps): JSX.Element => {
}, [userManager, skipSigninCallback, onSigninCallback]);

// register to userManager events
useEffect(() => {
React.useEffect(() => {
if (!userManager) return undefined;
// event UserLoaded (e.g. initial load, silent renew success)
const handleUserLoaded = (user: User) => {
Expand All @@ -254,42 +247,44 @@ export const AuthProvider = (props: AuthProviderProps): JSX.Element => {
};
}, [userManager]);

const removeUser = useCallback(
const removeUser = React.useCallback(
userManager
? () => userManager.removeUser().then(onRemoveUser)
: unsupportedEnvironment("removeUser"),
[userManager, onRemoveUser],
);

const signoutRedirect = useCallback(
const signoutRedirect = React.useCallback(
(args?: SignoutRedirectArgs) =>
userManagerContext.signoutRedirect(args).then(onSignoutRedirect),
[userManagerContext.signoutRedirect, onSignoutRedirect],
);

const signoutPopup = useCallback(
const signoutPopup = React.useCallback(
(args?: SignoutPopupArgs) =>
userManagerContext.signoutPopup(args).then(onSignoutPopup),
[userManagerContext.signoutPopup, onSignoutPopup],
);

const signoutSilent = useCallback(
const signoutSilent = React.useCallback(
(args?: SignoutSilentArgs) =>
userManagerContext.signoutSilent(args),
[userManagerContext.signoutSilent],
);

const contextValue = React.useMemo(() => {
return {
...state,
...userManagerContext,
removeUser,
signoutRedirect,
signoutPopup,
signoutSilent,
};
}, [state, userManagerContext, removeUser]);

return (
<AuthContext.Provider
value={{
...state,
...userManagerContext,
removeUser,
signoutRedirect,
signoutPopup,
signoutSilent,
}}
>
<AuthContext.Provider value={contextValue}>
{children}
</AuthContext.Provider>
);
Expand Down
18 changes: 18 additions & 0 deletions test/AuthProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,22 @@ describe("AuthProvider", () => {
mockSigninPopup.mockRestore();
});

it("should not update context value after rerender without state changes", async () => {
// arrange
const wrapper = createWrapper({ ...settingsStub });
const { result, rerender } = await act(async () => {
const { result, rerender } = renderHook(() => useAuth(), {
wrapper,
});
return { result, rerender };
});
const memoized = result.current;

// act
rerender();

// assert
expect(result.current).toBe(memoized);
});

});
Loading