Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we get Expire time along with tokens ? #178

Open
brunomolteni opened this issue Jun 26, 2020 · 3 comments
Open

Can we get Expire time along with tokens ? #178

brunomolteni opened this issue Jun 26, 2020 · 3 comments

Comments

@brunomolteni
Copy link

Hi there, love the library! it's being so useful on a client project... my only issue is the next:

When I set response: "tokens" , I obviously get the tokens back, but no way of knowing when those tokens will expire... can we get a new option for response ( could be expiry or expiration ) to get back that?

Thanks!

@simov
Copy link
Owner

simov commented Jun 26, 2020

By default Grant returns the tokens + the raw key. In case you use the response configuration explicitly then you can allow the raw key to be returned like this:

"response": ["tokens", "raw"]

Inside the raw key you get the raw data returned by the Authorization server. Most servers return expires_in key, and most of the time it's set to 3600 seconds.

The expiration of your token can be calculated like this:

var expires = Date.now() + (parseInt(raw.expires_in) * 1000)

Then the condition to check if the token has expired is:

if (Date.now() > expires) {
  // you have to refresh it
}

@simov
Copy link
Owner

simov commented Jun 27, 2020

Another approach is to set response: ['raw'] instead. The raw key contains everything returned from the authorization server, so you can get the tokens from there plus the expires_in key if present.

@carycodes
Copy link

Additionally, many services return a json web token as the access token. If you're using one of those services, you can skip the calculation by decoding the jwt and reading the expires timestamp directly. (In practice this doesn't matter much, since you'll want to refresh a couple minutes early to avoid getting messed up by skewed clocks or transport time.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants