I'm using a jwt token for authentication and would like to read the payload information on the client-side. Right now I'm doing something like this:
var payload = JSON.parse(window.atob(token.split('.')[1]));
Is there a better way to work with jwt tokens within the browser?
This simple solution returns raw token, header and the payload:
function jwtDecode(t) {
let token = {};
token.raw = t;
token.header = JSON.parse(window.atob(t.split('.')[0]));
token.payload = JSON.parse(window.atob(t.split('.')[1]));
return (token)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With