jwt-decode

Decode JWT tokens; useful for browser applications.

Showing:

Popularity

Downloads/wk

2.7M

GitHub Stars

2.3K

Maintenance

Last Commit

25d ago

Contributors

34

Package

Dependencies

0

Size (min+gzip)

0.8KB

License

MIT

Type Definitions

Built-In

Tree-Shakeable

Yes?

Reviews

Average Rating

4.8/56
Read All Reviews
rushabh10101
llk23r

Top Feedback

4Easy to Use
3Great Documentation
1Performant

Readme

jwt-decode is a small browser library that helps decoding JWTs token which are Base64Url encoded.

IMPORTANT: This library doesn't validate the token, any well formed JWT can be decoded. You should validate the token in your server-side logic by using something like express-jwt, koa-jwt, Owin Bearer JWT, etc.


Warning: When upgrading from version 2 to 3, there's a potentially breaking change

If you've previously imported the library as import * as jwt_decode from 'jwt-decode', you'll have to change your import to import jwt_decode from 'jwt-decode';.


auth0 logoIf you want to quickly add secure token-based authentication to your JavaScript projects, feel free to check Auth0's JavaScript SDK and free plan at auth0.com/developers

Installation

FOSSA Status

Install with NPM or Yarn.

Run npm install jwt-decode or yarn add jwt-decode to install the library.

Usage

import jwt_decode from "jwt-decode";

var token = "eyJ0eXAiO.../// jwt token";
var decoded = jwt_decode(token);

console.log(decoded);

/* prints:
 * { 
 *   foo: "bar",
 *   exp: 1393286893,
 *   iat: 1393268893  
 * }
 */

// decode header by passing in options (useful for when you need `kid` to verify a JWT):
var decodedHeader = jwt_decode(token, { header: true });
console.log(decodedHeader);

/* prints:
 * { 
 *   typ: "JWT",
 *   alg: "HS256" 
 * }
 */

Note: A falsy or malformed token will throw an InvalidTokenError error.

Use with typescript

The jwt_decode function will return an unknown type by default. You can specify what the expected return type should be by passing a type argument to the jwt_decode function.

The package also exports types for a JwtHeader and JwtPayload with some default claims. You can either use them as-is, or extend them to include non standard claims or properties.

import jwtDecode, { JwtPayload } from "jwt-decode";

const token: string = "eyJhsw5c";
const decoded = jwtDecode<JwtPayload>(token); // Returns with the JwtPayload type

Use as a CommonJS package

const jwt_decode = require('jwt-decode');
...

Include with a script tag

Copy the file jwt-decode.js from the build/ folder to your project somewhere, then include like so:

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

Older versions

If you want to use the library through Bower, an HTML import, use version v2.2.0. It has the same functionality.

Develop

Run npm run dev, this will fire up a browser and watch the /lib folder.

Issue Reporting

If you have found a bug or if you have a feature request, please report them at this repository issues section. Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.

Author

Auth0

License

This project is licensed under the MIT license. See the LICENSE file for more info.

FOSSA Status

Rate & Review

Great Documentation3
Easy to Use4
Performant1
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
rushabh1010155 Ratings82 Reviews
5 months ago

As provided by Auth 0 to decode any json web tokens while working on my project i have used it for sessions. As for every passed json web tokens it will directly return decoded fields.


0
ach51 Ratings52 Reviews
Software Engineer
5 months ago
Easy to Use

I used this official library by auth0 to decode JWT tokens in a personal weekend project. It's straightforward and returns the decoded fields for a passed JWT token.


0
Enrico Gerez Camponogara Da SilvaBrazil3 Ratings0 Reviews
Its not a bug, its a feature
4 months ago
Xelit3BCN20 Ratings3 Reviews
Xelit3 Open-Source profile
7 months ago
Easy to Use
Alaa SalehBuenos Aires, Argentina6 Ratings0 Reviews
7 months ago

Tutorials

No tutorials found
Add a tutorial