Skip to content

Instantly share code, notes, and snippets.

@LB-Digital
Created February 12, 2019 18:24
Embed
What would you like to do?
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