Node-jsonwebtoken: expiresIn is always 01/18/1970

Created on 12 Jun 2018  路  2Comments  路  Source: auth0/node-jsonwebtoken

This is how I am generating a JWT token

jwt.sign({}, 'a', {expiresIn: '30m'});

I send this token with every response. In the browser I am using the following code to decode the token.

//token is the complete JWT token generated by this module
let t = token.split('.')[1];
let exp = JSON.parse(atob(t)).exp
console.log(new Date(exp));

The exp is always set to 01/18/1970.

Most helpful comment

The expiry date is a NumericDate, which is the number of seconds since Epoch. Since Date takes milliseconds you need to multiply this by 1000.

So console.log(new Date(exp * 1000)) should show the correct date.

NumericDate

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. This is equivalent to the IEEE Std 1003.1, 2013 Edition [POSIX.1] definition "Seconds Since the Epoch", in which each day is accounted for by exactly 86400 seconds, other than that non-integer values can be represented. See RFC 3339 [RFC3339] for details regarding date/times in general and UTC in particular.

All 2 comments

The expiry date is a NumericDate, which is the number of seconds since Epoch. Since Date takes milliseconds you need to multiply this by 1000.

So console.log(new Date(exp * 1000)) should show the correct date.

NumericDate

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. This is equivalent to the IEEE Std 1003.1, 2013 Edition [POSIX.1] definition "Seconds Since the Epoch", in which each day is accounted for by exactly 86400 seconds, other than that non-integer values can be represented. See RFC 3339 [RFC3339] for details regarding date/times in general and UTC in particular.

Missed that completely. Thanks a lot.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dwelle picture dwelle  路  3Comments

usamamashkoor picture usamamashkoor  路  4Comments

BarukhOr picture BarukhOr  路  4Comments

yvele picture yvele  路  4Comments

prevostc picture prevostc  路  4Comments