NodeJS

Prerequisites

To be able to decode JWT token with JS you need to install jsonwebtoken package.

The best way to install it is through an npm package installer:

$ npm install jsonwebtoken

jsonwebtoken usage

First, you need to import the jsonwebtoken package:

const jwt = require('jsonwebtoken');

Second, we need to read and store a public key (used to decrypt a token). You need to get it from a dashboard beforehand and store somewhere near your project (./keys/id_rsa.pub).

const PUBLIC_KEY = fs.readFileSync('/path/to/public/key_public.pem'); // get public key

Then, we need to implement a function to verify a token using a public key:

function verifyJwt(token) {
    try {
        return jwt.verify(token, PUBLIC_KEY, { algorithms: ['RS256'] });
    }
    catch(e) {
        return;
    }
}

Result

If token is correct and did not expire, you'll receive a decoded JWT structure:

You can obtain challenge result in a result field.

Last updated

Was this helpful?