Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Library for decoding JWT on client-side

Tags:

javascript

jwt

I'm working on a website that stores JWT token inside cookies. What I want to do is, create Javascript that decodes the token and extracts the value I need and pass it on to the another Javascript written by my co-worker. My question is, is there client-side javascript library for JWT token decoding that I can import from my script?

like image 955
KMC Avatar asked May 13 '26 00:05

KMC


2 Answers

EDIT: It has come to my attention that this answer is incorrect. Please see this answer instead How to decode jwt token in javascript without using a library?

A JWT is just a dot separated base64 encoded string. You just need to split on the dots and then use atob() to decode. You don't need an external library.

e.g.

var jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ";

var tokens = jwt.split(".");

console.log(JSON.parse(atob(tokens[0])));
console.log(JSON.parse(atob(tokens[1])));
like image 60
bhspencer Avatar answered May 14 '26 13:05

bhspencer


https://github.com/auth0/jwt-decode : jwt-decode is a small browser library that helps decoding JWTs token which are Base64Url encoded.

like image 32
Ankita Avatar answered May 14 '26 15:05

Ankita



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!