Search code examples
adaladal.js

Unable to renew token with adal.js


I'm using Javascript (without Angular) to add AAD authentication to my single page web app. The initial login works fine, but after an hour, the token expires, and I am unable to renew it with acquireToken. I've tried calling acquireToken when I am still logged in with my clientID and it works fine, but after the token expires, I can't renew it. It fails with "Token renewal operation failed due to timeout".

After the token expires, I ran this:

// ADALContext created from calling new AuthenticationContext
// passed in same clientID to acquire token as to create ADALContext 
ADALContext.acquireToken(clientID, function (error, token) {console.log(error, token)})

I've enabled oauth2AllowImplicitFlow in AAD.

  "keyCredentials": [],
  "knownClientApplications": [],
  "logoutUrl": null,
  "oauth2AllowImplicitFlow": true,
  "oauth2AllowUrlPathMatching": true,

Not sure what step I'm missing. Thank you!

Edit: Right after the token expires, if I run acquireToken(clientID, func), I get "User login is required". However, if I call getCachedUser, I get a user back, after which calling acquireToken returned the timeout error.


Solution

  • Actually, adal for js will cache the login info into session storage or local storage in browser, depends your configuration in code. You can use chrome's develop tool to have a glance of this table: enter image description here

    So the cachaed user is decoded from adal.idtoken. So you can get the cached user. And according the source code of acquireToken, it will check whether the login user is exist before renew the access token, which will raise the User login is required issue.

    To bypass this issue, you can run getCachedUser() function before run acquireToken() for renewing an access token.