javascriptreactjsoauthoktaokta-auth-js

how to renew Okta token when claims change


So I have a working react app using oauth (using okta) and I have an issue.

When I change my claims in the authorization server the app does not see this until the token expires or I logoff and login again.

But when refresh the page(F5) I the getUser() function returns the correct claims but the token is not updated.

Is there a way to tell the okta client to fetch a new token ?

I am using okta-auth-js.

Thanks


Solution

  • So I found getWithoutPrompt() https://github.com/okta/okta-auth-js/blob/master/README.md#tokengetwithoutpromptoptions

    Which enables me to fetch a new token when I detect the change with the getUser() function

    authClient.token.getWithoutPrompt({
      responseType: 'id_token', // or array of types
      sessionToken: 'testSessionToken' // optional if the user has an existing Okta session
      scopes: [
        'openid',
        'email',
        'profile'
      ],
      state: '8rFzn3MH5q',
      nonce: '51GePTswrm',
      // Use a custom IdP for social authentication
      idp: '0oa62b57p7c8PaGpU0h7'
     })
    .then(function(res) {
      var tokens = res.tokens;
    
      // Do something with tokens, such as
      authClient.tokenManager.setTokens(tokens);
    })
    .catch(function(err) {
      // handle OAuthError or AuthSdkError (AuthSdkError will be thrown if app is in OAuthCallback state)
    });