Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create JWT exp style date in Javascript

I'll like to create JWT exp claim style date in Javascript. My app jwt claim returns an expiry date of 1424984529. I'm doing a test for token expiration using this:

if(jwt.exp < Date.now())) {
  // do something
}

As at writing Date.now() gives me 1424941329632 and jwt.exp gives 1424984529. Obviously, my test will always return true.

So my question is, how do I mimic jwt-style exp date in Javascript?

like image 939
drecute Avatar asked Feb 26 '15 09:02

drecute


People also ask

What is the format of exp in JWT?

A JSON numeric value representing the number of seconds from 1970-01-01T00:00:00Z UTC until the specified UTC date/time, ignoring leap seconds.

How do I set the expiry time on my JWT token?

Go to Dashboard > Applications > APIs and click the name of the API to view. Locate the Token Expiration (Seconds) field, and enter the appropriate access token lifetime (in seconds) for the API. Default value is 86,400 seconds (24 hours). Click Save Changes.

What is IAT and EXP?

exp (expiration time): Time after which the JWT expires. nbf (not before time): Time before which the JWT must not be accepted for processing. iat (issued at time): Time at which the JWT was issued; can be used to determine age of the JWT.

Does JWT decode check expiration?

Expiration time is automatically verified in jwt. decode() and raises jwt.


1 Answers

How about:

if (jwt.exp < Date.now() / 1000) {
  // do something
}
like image 79
Hans Z. Avatar answered Sep 17 '22 17:09

Hans Z.