okta / okta-auth-js

The official js wrapper around Okta's auth API
Other
454 stars 265 forks source link

Subscribes a callback that will be called when the authStateChange event happens. not triggering after 1hr token expiration #1548

Open juztinlazaro opened 3 weeks ago

juztinlazaro commented 3 weeks ago

Describe the bug

Subscribes a callback that will be called when the authStateChange event happens. not triggering after 1hr token expiration or idle mode

version:

    "@okta/okta-auth-js": "7.8.1",
    "@okta/okta-react": "6.9.0",
    "@okta/okta-signin-widget": "7.24.2",

Reproduction Steps?

export const getOktaAuth = () => {
  return new OktaAuth({
    clientId: OKTA_CLIENT_ID,
    redirectUri: `${window.location.origin}/authorization-code/callback`,
    useClassicEngine: true,
    issuer: `${OKTA_API_BASE}/oauth2/${OKTA_AUTH_SERVER}`,
    pkce: true,
    url: OKTA_API_BASE,
    tokenManager: {
      storageKey: 'mycrm-tokens',
    },
    transformAuthState: async (oktaAuth, authState) => {
      if (!authState.isAuthenticated || !!authState.oktaUser) {
        return authState;
      }
      const user = await oktaAuth.token.getUserInfo();
      authState.isAuthenticated = !!user;
      authState.oktaUser = user;
      return authState;
    },
  });
};
export const onRefreshExpirationToken = async () => {
 // trigger every 1hr
  const oktaAuth = getOktaAuth();

  oktaAuth.authStateManager.subscribe(async (authState) => {
    console.log("authState==", authState);
    if (authState.isAuthenticated) {
      const newToken = authState.accessToken?.accessToken;
      LocalStorageProxy.token = newToken;
    }
  });

  oktaAuth.authStateManager.updateAuthState();
};

SDK Versions

version:

    "@okta/okta-auth-js": "7.8.1",
    "@okta/okta-react": "6.9.0",
    "@okta/okta-signin-widget": "7.24.2",

Additional Information?

No response

juztinlazaro commented 2 weeks ago

fixed by adding oktaAuth.start(); https://github.com/okta/okta-auth-js?tab=readme-ov-file#running-as-a-service