Last active
April 24, 2018 02:57
-
-
Save msteckyefantis/4d0680695db5c5cfe4119e7d58770ba2 to your computer and use it in GitHub Desktop.
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
function getDecodedJWTComponents( token ) { | |
const periodSplitToken = token.split( '.' ); | |
const endcodedJWTHeader = periodSplitToken[0]; | |
const header = getDecodedJWTComponent( endcodedJWTHeader ); | |
const endcodedJWTPayload = periodSplitToken[1]; | |
const payload = getDecodedJWTComponent( endcodedJWTPayload ); | |
return { | |
header, | |
payload | |
}; | |
} | |
// helper function | |
function getDecodedJWTComponent( encodedTokenComponent ) { | |
const tokenComponentAsBuffer = new Buffer( encodedTokenComponent, 'base64' ); | |
const tokenComponentAsString = tokenComponentAsBuffer.toString( 'ascii' ); | |
const decodedTokenComponent = JSON.parse( tokenComponentAsString ); | |
return decodedTokenComponent; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related to this article about JWT.