Created
June 24, 2022 22:37
-
-
Save babacarMbengue12/518574819ae30f7de7f639f4c388d5dc to your computer and use it in GitHub Desktop.
javascript verify if user token is expired
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
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 | |
} |
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
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