hd-utils
Version:
A handy utils for modern JS developers
17 lines (16 loc) • 596 B
JavaScript
/**
* @description The function takes a JWT token and returns the decoded payload as a JSON object.
* @param {string} token - The JWT token that you want to parse.
* @returns The JSON payload of the JWT.
*/
export default function parseJwt(token) {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
var jsonPayload = decodeURIComponent(atob(base64)
.split('')
.map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
})
.join(''));
return JSON.parse(jsonPayload);
}