How to decode client side JWT token payload?

I am using the jwt token for authentication and would like to read the client side payload information. Now I am doing something like this:

var payload = JSON.parse(window.atob(token.split('.')[1])); 

Is there a better way to work with jwt tokens in a browser?

+4
source share
2 answers

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

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

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

var token = 'eyJ0eXAiO.../// jwt token';
var decoded = jwt_decode(token);
console.log(decoded);
+7
source

This simple solution returns the original token, header and 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)
}
+1
source

Source: https://habr.com/ru/post/1653000/


All Articles