Categories
JavaScript Answers

How to check if token expired using this JWT library in Node.js?

Spread the love

To check if token expired using this JWT library in Node.js, we get the expiration timestamp and compare it to the current date and time.

For instance, we write

const isTokenExpired = (token) =>
  Date.now() >= JSON.parse(atob(token.split(".")[1])).exp * 1000;

to get the token’s expiration timestamp in milliseconds with

JSON.parse(atob(token.split(".")[1])).exp * 1000

Then we compare it to the current timestamp returned by Date.now.

If the current timestamp is bigger than the token’s timestamp, then the token expired.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *