JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2864
  • Score
    100M100P100Q122567F
  • License Apache-2.0

JavaScript library that decodes JSON Web Tokens (JWT) jwt-js-decode the easy way

Package Exports

  • jwt-js-decode
  • jwt-js-decode/dist/jwt-js-decode.esm.js
  • jwt-js-decode/dist/jwt-js-decode.min.js
  • jwt-js-decode/dist/jwt-js-decode.umd.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (jwt-js-decode) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

jwt-js-decode

JavaScript library that decodes JSON Web Tokens (JWT) jwt-js-decode the easy way

Use cases

Works in:

  • all modern browsers that support WebCrypto API
  • Node.js using 'crypto' (default node-js lib)
  • any other js env using 'crypto-browserify' (with require replacement to 'crypto'')

Getting Started

Install Jest using yarn:

yarn add jwt-js-decode

Or via npm:

npm i -S jwt-js-decode

Require lib

import * as jwtJsDecode from 'jwt-js-decode';

or

const jwtJsDecode = require('jwt-js-decode');

or

<script src="//unpkg.com/jwt-js-decode@1.5.1/dist/jwt-js-decode.pkg.min.js"></script>

Other links you can find on yarn

P.S. For Webpack 5 browser bundling, there is need to add crypto fallback config (example stackblitz):

resolve: {
  fallback: {
    crypto: false,
  },
},

Usage

    import { jwtDecode, jwtVerify, resignJwt } from 'jwt-js-decode';
    
    // just decode 'token' into {header: Object, payload: Object, signature: String}
    let jwt = jwtDecode('token');
    console.log(jwt.payload);

    // or verify 'token' with provided secret and decode it
    jwtVerify('token', 'secret').then(res => {
        if (res === true) {
            const jwt = jwtDecode('token');
            console.log(jwt.payload);
        }
    });
    
    // advanced resignJwt token with newSecret secret should be same type as jwt.header.alg
    resignJwt('token', 'newSecret').then(newToken =>  {
        console.log(newToken);
    });

Sandboxes

Runkit (node.js sandbox 1.5.1) https://npm.runkit.com/jwt-js-decode

Plunkr (browser sandbox 1.5.1 + CodeMirror) https://plnkr.co/edit/WaawWXswkexfXaFfRDjQ

Plunkr (browser sandbox 1.5.1) https://plnkr.co/edit/zCNpiTXBbPKNxNVnHhkU

Development

First you'll need to clone this repository to folder you will test it in.

Then insall all dependencies.

npm i

For testing functionality jest testing is used, npm test script will rebuild dist and docs folder.

After changes in jwt-js-decode source and just for the first run use:

npm test

For sequential runs and just new test you can run:

npm run jest

TOC: