fix(ui): potential fix for okta login, clear state for avoid login errors (#20379)

* fix(ui): okta login clear state for avoid errors

* add fallback option
This commit is contained in:
Chirag Madlani 2025-03-21 20:35:15 +05:30 committed by GitHub
parent d317081460
commit fc30e69442
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -47,6 +47,10 @@ export const OktaAuthProvider: FunctionComponent<Props> = ({
tokenManager: {
autoRenew: false,
},
cookies: {
secure: true,
sameSite: 'none',
},
}),
[clientId, issuer, redirectUri, scopes, pkce]
);
@ -55,7 +59,21 @@ export const OktaAuthProvider: FunctionComponent<Props> = ({
await oktaAuth.signInWithRedirect();
};
const restoreOriginalUri = useCallback(async (_oktaAuth: OktaAuth) => {
const customAuthHandler = async () => {
const previousAuthState = oktaAuth.authStateManager.getPreviousAuthState();
if (!previousAuthState?.isAuthenticated) {
// Clear storage before triggering login
oktaAuth.tokenManager.clear();
await oktaAuth.signOut();
// App initialization stage
await triggerLogin();
} else {
// Ask the user to trigger the login process during token autoRenew process
}
};
const restoreOriginalUri = useCallback(
async (_oktaAuth: OktaAuth) => {
const idToken = _oktaAuth.getIdToken() ?? '';
const scopes =
_oktaAuth.authStateManager.getAuthState()?.idToken?.scopes.join() || '';
@ -77,21 +95,15 @@ export const OktaAuthProvider: FunctionComponent<Props> = ({
};
onLoginSuccess(user);
})
.catch((err) => {
.catch(async (err) => {
// eslint-disable-next-line no-console
console.error(err);
// Redirect to login on error
await customAuthHandler();
});
}, []);
const customAuthHandler = async () => {
const previousAuthState = oktaAuth.authStateManager.getPreviousAuthState();
if (!previousAuthState || !previousAuthState.isAuthenticated) {
// App initialization stage
await triggerLogin();
} else {
// Ask the user to trigger the login process during token autoRenew process
}
};
},
[onLoginSuccess]
);
return (
<Security