Created
May 3, 2020 13:20
-
-
Save smitroshin/47f4609a09628f55aa478ced8b04a78e to your computer and use it in GitHub Desktop.
Decode JWT token
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
/** | |
* Decode JWT token | |
* | |
* @param {string} token | |
* @return {object} | |
*/ | |
const parseJwt = (token) => { | |
const base64Url = token.split('.')[1]; | |
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/'); | |
const jsonPayload = decodeURIComponent( | |
atob(base64) | |
.split('') | |
.map(function (c) { | |
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); | |
}) | |
.join(''), | |
); | |
return JSON.parse(jsonPayload); | |
}; | |
export default parseJwt; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment