Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSAL how to read token's expiry date?

Do you have any idea if there is a way to check if token has expired in Msal (in order to know if should get the acquireTokenSilent or not) Thanks

like image 513
Ile G Avatar asked Sep 01 '25 04:09

Ile G


1 Answers

You don't need to check this by yourself. In MSAL, you will call acquireTokenSilent method to make a silent request(without prompting the user) to Azure AD to obtain an access token. MSAL will automatically refresh your access token after expiration when calling.

If the silent token acquisition fails for some reasons such as an expired token or password change, you will need to invoke an interactive method to acquire tokens such as acquireTokenPopup or acquireTokenRedirect.

var graphScopes = ["user.read", "mail.send"];

   userAgentApplication.loginPopup(graphScopes).then(function (idToken) {
       //Login Success
       userAgentApplication.acquireTokenSilent(graphScopes).then(function (accessToken) {
           //AcquireTokenSilent Success
       }, function (error) {
           //AcquireTokenSilent Failure, send an interactive request.
           userAgentApplication.acquireTokenPopup(graphScopes).then(function (accessToken) {
               updateUI();
           }, function (error) {
               console.log(error);
           });
       })
   }, function (error) {
       //login failure
       console.log(error);
   });

You can refer to this article for more details.

like image 170
Tony Ju Avatar answered Sep 02 '25 17:09

Tony Ju