Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decode JWT Token payload on client side?

Tags:

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?

like image 773
Woodsy Avatar asked Aug 30 '16 13:08

Woodsy


1 Answers

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)
}
like image 74
Prcek Avatar answered Sep 28 '22 03:09

Prcek