Skip to content

Instantly share code, notes, and snippets.

@babacarMbengue12
Created June 24, 2022 22:37
Show Gist options
  • Save babacarMbengue12/518574819ae30f7de7f639f4c388d5dc to your computer and use it in GitHub Desktop.
Save babacarMbengue12/518574819ae30f7de7f639f4c388d5dc to your computer and use it in GitHub Desktop.
javascript verify if user token is expired
import jwtDecode from "jwt-decode";
function dateFromUnix(unix){
return new Date(unix * 1000)
}
function isTokenExpired(token: string,unixField="exp"){
if (token) {
const decoded = jwtDecode(token);
const exp = decoded[unixField]
return dateFromUnix(exp) < new Date()
}
return true
}
import jwtDecode from "jwt-decode";
function dateFromUnix(unix: number){
return new Date(unix * 1000)
}
function isTokenExpired(token: string,unixField="exp"){
if (token) {
const decoded: any = jwtDecode(token);
const exp = decoded[unixField] as number
return dateFromUnix(exp) < new Date()
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment