Skip to content

Instantly share code, notes, and snippets.

@aasumitro
Created December 29, 2021 09:20
Show Gist options
  • Save aasumitro/18c07817a04b88c5927a2c07b9c7f549 to your computer and use it in GitHub Desktop.
Save aasumitro/18c07817a04b88c5927a2c07b9c7f549 to your computer and use it in GitHub Desktop.
// 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