Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Check if the Date is already Expired in Token [duplicate]

Good day,

I'm using jwt in my authentication. I already decoded my token but the problem is, I want to check if the token exp is already expired or not.

var decodedToken = localStorage.getItem('user_token');

console.log(decodedToken.exp) // writes 1540360205

Thank you in advance.

like image 774
jsonGPPD Avatar asked Oct 23 '18 06:10

jsonGPPD


2 Answers

It appears that the exp claim of your JWT tokens is bearing a UNIX timestamp, in seconds. To check if a given JWT is expired then you can just compare against the current date as a UNIX timestamp:

var decodedToken = localStorage.getItem('user_token');
if (decodedToken.exp < new Date()/1000) {
    console.log("EXPIRED");
}
like image 148
Tim Biegeleisen Avatar answered Nov 15 '22 05:11

Tim Biegeleisen


This should get you the local time then you can compare it with current date and time and check if the token has expired

var tokenDate = new Date(parseInt(localstorage.getItem('user_token')) * 1000)
like image 41
Nikhil Prasad Avatar answered Nov 15 '22 05:11

Nikhil Prasad