accessToken is not coming instantly with idToken

Hi, I am able to use Okta Auth js sdk for authentication and authorization but I am facing error while accessing accessToken from localStorage. idToken comes instantly after the api call in local storage but accessToken comes after 10 minutes delay may be even after a bit. But in the api call accessToken is coming instantly in network call. Could you please tell me the error or what I am missing in the implementation? Since in the TokenManager access token is coming after delay, I am not able to use it properly. Thanks in advance.

That doesn’t really make sense to me. What OAuth flow are you using? When you check the network events in the browser, do you see both tokens coming back together in the same request?

Also, which SDK(s) are you using: are you only use AuthJS or are you using one of our framework SDKs (React, Angular, Vue)?

Hi Andrea, I am using only AuthJS based on our requirement. I need accessToken and idToken both but I am getting only idToken and after sometime lag (after 10 minutes or something) I am getting accessToken in the local storage. In the network call I am getting the accessToken and idToken together but accessToken is not getting stores instantly like idToken. I am using this code using getWithRedirect() from the official website. Please help me

// Bootstrap the AuthJS Client
const authClient = new OktaAuth({
  // Org URL
  url: 'https://${yourOktaDomain}',
  // OpenID Connect APP Client ID
  clientId: '${clientId}',
  // Trusted Origin Redirect URI
  redirectUri: 'http://localhost:8080'
});

if (authClient.isLoginRedirect()) {
  // Parse token from redirect url
  authClient.token.parseFromUrl()
    .then(data => {
      const { idToken } = data.tokens;
      console.log(`Hi ${idToken.claims.email}!`);
      // Store parsed token in Token Manager
      authClient.tokenManager.add('idToken', idToken);
      console.log(idToken);
    });
} else {
  // Attempt to retrieve ID Token from Token Manager
  authClient.tokenManager.get('idToken')
    .then(idToken => {
      console.log(idToken);
      if (idToken) {
        console.log(`Hi ${idToken.claims.email}!`);
      } else {
        // You're not logged in, you need a sessionToken
        authClient.token.getWithRedirect({
          responseType: ["token", "id_token"],
        });
      }
    })
}

Are you using implicit flow to get both the id token and the access token? If so, why aren’t you storing both the access and ID tokens in the tokenManager when the promise for parseFromUrl resolves (in the function inside .then) instead of just the idToken?

I am not using implicit flow.I am using PKCE flow. Now it worked based on your suggestion. Thank you.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.