question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do you re-validate the token for session management?

See original GitHub issue

Core Library

MSAL.js v2 (@azure/msal-browser)

Core Library Version

2.13.1

Wrapper Library

Not Applicable

Wrapper Library Version

NA

Public or Confidential Client?

Public

Description

I have a single page React app. I use this @azure/msal-browser package’s loginPopup() API to let my app user login using their Azure AD credentials. Once successfully signed in, their login data e.g. access token etc gets stored in the local storage.

My question is how do I check the validity of the access token for the. session? i.e. if the token has passed its expiration time? Do I can see the MSAL response does have “exp”: 1662430649, (Unix timestamp) value of expiration. Do I save this in the local storage and check\update on each user interaction or save session data in the backend database and do the validations? Does the MS Graph API or the @azure/msal-browser package provides any API to validate the expiry of the token?

MSAL Configuration

Not relevant to my question

Relevant Code Snippets

Not applicable

Identity Provider

Azure AD / MSA

Source

External (Customer)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
bmahallcommented, Sep 14, 2022

@DewangS Thanks for elaborating further. As @derisen mentioned above, the idToken is not tied to the web resource that you are trying to access. In order to renew id tokens, you need to use acquireTokenSilent with the client-id of your application as the scope. This api will automatically fetch a valid IdToken for you in case of expiration.

If the session is not valid at that point, Msal will throw an error. You can then catch the error & use acquireTokenRedirect() or acquireTokenPopup() for renewing the token. Since the Access token lifetime is 1h, you can also make sure you call the ATS api every hour, to maintain the session.

Please let us know in case you have any further questions.

1reaction
DewangScommented, Sep 15, 2022

Thanks @bmahall for the detailed explanation. For now I’ve just implemented local session management i.e. if the session is idle for a set amount of time, user gets logged out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I validate session tokens and obtain session details ...
You can inspect your own token by passing it in the tokenId parameter or in the iPlanetDirectoryPro header. If you don't specify the...
Read more >
Validating session tokens - Clerk.dev
Retrieve the session token from either __session cookie or from the Authorization header · Use the above Public Key to verify the token's...
Read more >
Is this a right technique to create and validate session tokens?
Current token format, creation, verification: · Check if it has prefix. · Slice it to get token without prefix · Decode it to...
Read more >
Session Management - OWASP Cheat Sheet Series
The session ID or token binds the user authentication credentials (in the form of a user session) to the user HTTP traffic and...
Read more >
Introducing JWTs for session management - Stytch
To authenticate a user, developers can either implement a JWT or session token-based solution. A JSON Web Token (or JWT) is an open...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found