Created
December 29, 2021 09:20
-
-
Save aasumitro/18c07817a04b88c5927a2c07b9c7f549 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TESTING | |
const token = localStorage.getItem('token') ?? null | |
if (token != null) { | |
// [START] JWT_DECODE | |
// refactore to one function e.g decodeJwt | |
let base64Url = token.split('.')[1]; | |
let base64 = base64Url.replace('-', '+').replace('_', '/'); | |
let decodedData = JSON.parse(Buffer.from(base64, 'base64').toString('binary')); | |
// [END] JWT_DECODE | |
// [START] COMPARE EXPIRED TIME | |
const now = new Date() | |
const exp = new Date(decodedData.exp) | |
console.log(`from jwt: ${exp}`) | |
console.log(`current time: ${now}`) | |
if (exp >= now) { | |
// if token expired redirect to | |
console.log("token is expired") | |
} | |
// [END] COMPARE EXPIRED TIME | |
console.log("next") | |
} | |
// TESTING |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment