How to decode JWT Token payload on client side?

From https://github.com/auth0/jwt-decode

download .build/jwt-decode.min.js file and include in the project.

<script src="js/jwt-decode.min.js"></script>

var token = 'eyJ0eXAiOo876jgJ96...'; // jwt token;
var decoded = jwt_decode(token);
console.log(decoded);

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)
}

Tags:

Javascript

Jwt