Skip to content

Instantly share code, notes, and snippets.

@LB-Digital
Created February 12, 2019 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LB-Digital/17b41286f66ee48e7c78ab0d957efd99 to your computer and use it in GitHub Desktop.
Save LB-Digital/17b41286f66ee48e7c78ab0d957efd99 to your computer and use it in GitHub Desktop.
Retrieve the Payload data section of a JSON Web Token.
function getJwtPayload(){
//retrieve JsonWebToken from cookies
var cookies = document.cookie.split(";"),
jwt;
for (cookie of cookies){
var keyValue = cookie.trim().split('=');
if (keyValue[0] === "jwt"){
jwt = keyValue[1];
}
}
//extract user data from JsonWebToken
var payload = jwt.split('.')[1]; // middle section = payload
var decodedPayload = JSON.parse(atob(payload));
return decodedPayload['data'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment