Here is a sample code:
Code given above is the final version. Initially I was using
getCachedUser() instead of
getCachedToken(). In that case if the browser window was left untouched for more than an hour, acquireToken was throwing error:
Token Renewal operation failed due to timeout
I found an answer to this in an issue logged on github here.
So what happens when use,
getUser methods look into the browser storage for id_token and returns a non-null user if there is a token inside the cache. It does not look into the token expiration time.
So when localStorage is used, tokens are preserved in the cache, when one re-opens the browser (and hence getCachedUser returns a non-null object) but the AAD cookie is expired (unless user checked the
keep me signed in checkbox when logging in). Since the cookie is expired, acquire token call fails with the “login required” error.
So until they fix
getCachedUser to check for token validity we are better off using
Until next time.