nodejs-apponlytoken-rest-sample icon indicating copy to clipboard operation
nodejs-apponlytoken-rest-sample copied to clipboard

How to handle an expired token

Open linuxbandit opened this issue 10 years ago • 2 comments

Hello hello,

I was checking out the other example and comparing it with this one, and I noticed a couple of things:

  • Unlike the other example, in this grant flow there is no such thing as "Refresh Token"
  • Unlike the other, more complete example, here it is not handled the case when the token has expired. In the other one, before every call there was a smart function that would check beforehand if the token needed be refreshed.

Since I am just learning about Oauth -mainly from this use case- I was wondering how should one proceed in this case -- especially in a case where there is no such Refresh Token.

linuxbandit avatar Feb 18 '16 11:02 linuxbandit

Hey, @linuxbandit. Sorry for the late response. The notification for this issue went under the radar.

The samples are fundamentally different. This sample demonstrates how to connect a basic service (no user interaction) to Microsoft Graph.

In this sample, there's no need to implement the refresh flow. An application can simply request another access token. Tenant administrators consent these applications, thus, there's no need for end-user interaction.

For more information, see Build service and daemon apps in Office 365.

Let me know if you have more questions.

ricalo avatar May 10 '16 22:05 ricalo

Thank you, yes I also forgot to reply to my own question for those who needed, but I have found the solution and I wrote this snippet:

function checkIfTokenExpired(token, callback) {
    if( new Date(TOKEN_EXPIRE_ON) - new Date() < 10000){ //100 seconds
      console.log("Requesting another token: less than 100s to expiration");
      return authenticate(callback);
    }
    return callback(token);
};

So, if token is about to expire, I ask for another one and then the callback is called, otherwise the callback is directly invoked

linuxbandit avatar May 11 '16 08:05 linuxbandit