Skip to content

Commit

Permalink
Merge pull request #2715 from florkbr/fix-stale-user-ref-in-chrome-utils
Browse files Browse the repository at this point in the history
OIDC migration hot fix: fix stale user reference when using chrome utils
  • Loading branch information
florkbr authored Nov 30, 2023
2 parents bbf6b35 + 58c8ae8 commit a1449e2
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/auth/OIDCConnector/OIDCSecured.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import { hasAuthParams, useAuth } from 'react-oidc-context';
import { User } from 'oidc-client-ts';
import { BroadcastChannel } from 'broadcast-channel';
Expand Down Expand Up @@ -75,33 +75,34 @@ export function OIDCSecured({
setCookieElement,
}: React.PropsWithChildren<{ microFrontendConfig: Record<string, any> } & FooterProps>) {
const auth = useAuth();
const authRef = useRef(auth);
const store = useStore();
const dispatch = useDispatch();
const [state, setState] = useState<ChromeAuthContextValue>({
ready: false,
logoutAllTabs: (bounce = true) => {
authChannel.postMessage({ type: 'logout' });
logout(auth, bounce);
logout(authRef.current, bounce);
},
logout: () => {
logout(auth, true);
logout(authRef.current, true);
},
login: (requiredScopes) => login(auth, requiredScopes),
login: (requiredScopes) => login(authRef.current, requiredScopes),
loginAllTabs: () => {
authChannel.postMessage({ type: 'login' });
},
getToken: () => Promise.resolve(auth.user?.access_token ?? ''),
getToken: () => Promise.resolve(authRef.current.user?.access_token ?? ''),
getOfflineToken: () =>
getOfflineToken(
auth.settings.metadata?.token_endpoint ?? '',
auth.settings.client_id,
encodeURIComponent((auth.settings.metadata?.token_endpoint ?? '').split('#')[0])
authRef.current.settings.metadata?.token_endpoint ?? '',
authRef.current.settings.client_id,
encodeURIComponent((authRef.current.settings.metadata?.token_endpoint ?? '').split('#')[0])
),
doOffline: () => login(auth, ['offline_access'], prepareOfflineRedirect()),
getUser: () => Promise.resolve(mapOIDCUserToChromeUser(auth.user ?? {}, {})),
token: auth.user?.access_token ?? '',
tokenExpires: auth.user?.expires_at ?? 0,
user: mapOIDCUserToChromeUser(auth.user ?? {}, {}),
doOffline: () => login(authRef.current, ['offline_access'], prepareOfflineRedirect()),
getUser: () => Promise.resolve(mapOIDCUserToChromeUser(authRef.current.user ?? {}, {})),
token: authRef.current.user?.access_token ?? '',
tokenExpires: authRef.current.user?.expires_at ?? 0,
user: mapOIDCUserToChromeUser(authRef.current.user ?? {}, {}),
});

const startChrome = async () => {
Expand Down Expand Up @@ -169,6 +170,10 @@ export function OIDCSecured({
}
}, [auth]);

useEffect(() => {
authRef.current = auth;
}, [auth]);

if (!auth.isAuthenticated || !state.ready) {
return <AppPlaceholder cookieElement={cookieElement} setCookieElement={setCookieElement} />;
}
Expand Down

0 comments on commit a1449e2

Please sign in to comment.