Created
May 27, 2022 12:36
-
-
Save imlana21/bdade15e1b6d71d261f611730e50a58c to your computer and use it in GitHub Desktop.
JWT Token Decode Header and Payload to JSON
This file contains 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
export default function jwtDecode(token: any) { | |
const base64Urls = token.split('.'); | |
base64Urls.map( (url: string, i: number) => { | |
if(i<2) | |
base64Urls[i].replace(/-/g, '+').replace(/_/g, '/'); | |
}); | |
const jwtHeader = JSON.parse(Buffer.from(base64Urls[0], 'base64').toString()); | |
const jwtPayload = JSON.parse(Buffer.from(base64Urls[1], 'base64').toString()); | |
return [ | |
jwtHeader, | |
jwtPayload | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment