Juan
Juan

Reputation: 139

How to set json web token expire and validate

The problem is that when comparing both dates the token would expire in approximately 17 hours, which is wrong, since at the time of generation it should be only 1 minute longer.

If someone can help me I appreciate it very much

Upvotes: 0

Views: 2632

Answers (1)

Chris Brenberg
Chris Brenberg

Reputation: 142

It looks like your code assumes that the expiration and issued at properties are defined as milliseconds (60000 milliseconds = 60 seconds = 1 minute).

However, the JWT specification states that exp and iat use 'Seconds since the Epoch. See the answer here: https://stackoverflow.com/a/39926886/12086953

So just change the value in your first code snippet to expire in 60 seconds rather than 60000 seconds (which is just under 17 hours), like this:

const myToken = jwt.sign({ id: '12345677', username: '[email protected]' }, 'mysecretjsonwebtoken', { expiresIn: 60, audience: '12345677' })

Upvotes: 1

Related Questions