JWT Toolkit

Modules

JWTRequest

JWTRequest()

function JWTRequest(requestFunction: (jwt: string) => Promise, renewTokenFunction: (err: *) => Promise.<string>, initialJwt: string): Promise

JWTRequest is designed to help with trying and retrying XHR requests using JWT. As JWTs can expire without the front-end knowing about it, inevitably requests are sometimes sent with expired JWTs. The back end will often respond with an appropriate error and status code to indicate that the token has expired. The front-end must then request a new token and retry the original request (or set of requests) with the new token. This function handles that try-fail-renew-retry pattern.

It does not make XHR requests itself, it merely provides hooks.

Any new responses that arrive while a token is being renewed will be queued and fired once the new token is received.

const requestFunction: Function = (jwt: string): Promise<*> => {
     const headers = Token.asHeaders(jwt);
     return coolRequest(headers);
};

const renewTokenFunction: Function = (err: *): ?Promise<string> => {
    // example: in this case the server indicates that your token has expired (and your coolRequest has failed) by returning a 401 error
    if(err && err.status == 401) {
        return renewMyTokenRequest();
    }
};

JWTRequest(requestFunction, renewTokenFunction, "myTokenStringHere");
Params
  • requestFunction: (jwt: string) => Promise -

    A function that makes the request you want when. It is passed the JWT string to use in your request, and must return a promise that should be resolved / rejected when your request resolves / rejects.

  • renewTokenFunction: (err: *) => Promise.<string> -

    A function that is called when a request returns an error. Using the error info provided you can then makes a request to get a new JWT. This function should return a promise containing the new JWT string, or a falsey value if you don't want to renew the token.

  • initialJwt: string -

    The JWT to attempt to use for the initial request.

Returns
  • Promise -

    A promise that will be resolved once the request is complete, after any necessary renewing and retrying. It will be rejected if the response indicates an error has occured (not counting errors due to expired tokens).

Token

isValid()

function isValid(token: string): boolean

Tests if the token is valid.

import Token from 'jwt-toolkit';
Token.isValid("NOT A TOKEN");
// returns false
Params
  • token: string -

    JWT string

Returns
  • boolean -

    A boolean indicating if the token is valid (true) or not (false)

isExpired()

function isExpired(token: string): boolean

Tests if the token is expired according to the client's clock. Be careful with this one, as it can be incorrect if you don't compensate for possible differences between the client's clock and the server's.

import Token from 'jwt-toolkit';
Token.isExpired("eyJhbGciOiJIU...");
Params
  • token: string -

    JWT string

Returns
  • boolean -

    A boolean indicating if the token is expired / invalid (true) or has time remaining (false)

asHeaders()

function asHeaders(token: string): Object

Returns an object containing JWT as authorization headers.

import Token from 'jwt-toolkit';
Token.asHeaders("eyJhbGciOiJIU...");
// returns { Authorization: 'Bearer eyJhbGciOiJIU...' }
Params
  • token: string -

    JWT string

Returns
  • Object -

    Headers object with JWT as authorization header