Skip to content

Commit

Permalink
fix(oidc): unload user on logout (#1296) (release)
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-chervet authored Feb 13, 2024
1 parent 8585205 commit 0d13cc2
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions packages/react-oidc/src/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ export const useOidcUser = <T extends OidcUserInfo = OidcUserInfo>(configuration
const [oidcUser, setOidcUser] = useState<OidcUser<T>>({ user: null, status: OidcUserStatus.Unauthenticated });
const [oidcUserId, setOidcUserId] = useState<string>('');

const oidc = OidcClient.get(configurationName);

useEffect(() => {
const oidc = OidcClient.get(configurationName);
let isMounted = true;
if (oidc && oidc.tokens) {
setOidcUser({ ...oidcUser, status: OidcUserStatus.Loading });
Expand All @@ -34,7 +35,18 @@ export const useOidcUser = <T extends OidcUserInfo = OidcUserInfo>(configuration
} else {
setOidcUser({ user: null, status: OidcUserStatus.Unauthenticated });
}
return () => { isMounted = false; };
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const newSubscriptionId = oidc.subscribeEvents((name: string, data: any) => {
if (name === OidcClient.eventNames.logout_from_another_tab || name === OidcClient.eventNames.logout_from_same_tab) {
if (isMounted) {
setOidcUser({ user: null, status: OidcUserStatus.Unauthenticated });
}
}
});
return () => {
isMounted = false;
oidc.removeEventSubscription(newSubscriptionId);
};
}, [oidcUserId]);

const reloadOidcUser = () => {
Expand Down

0 comments on commit 0d13cc2

Please sign in to comment.