A JS library for managing JWT tokens
JWT Manager is a Javascript Library designed to make storing and retrieving JWT tokens easier. It has an easy to use API
designed to make working with JSON Web Tokens much easier. It is written using Typescript and has no dependencies.
You can install this package by running npm install --save jwt-manager
.
You can then import the library using the following:
import JWTManager from 'jwt-manager';
window.JWTManager = new JWTManager();
Using the JWT Manager library you can set, get, forget and refresh JWT Tokens. By default they are stored as a cookie, however you can change this to local storage if you would prefer.
To change the store to local storage, simply use the following:
window.JWTManager.useLocalStore();
Once you retrieve a valid JWT Token from your server, you can store the JWT token by using the following method call: JWTManager.setToken('jwt-token')
. This will then store the JWT Token in the storage driver chosen (defaults to cookie).
Once you have set a JWT Token, you can easily retrieve it at any time by using the following method call JWTManager.getToken()
. This will then return the JWT Token.
If you would like to remove the stored JWT Token, you can simply call the forget
method as follows: JWTManager.forget()
.
To refresh a JWT Token, you simply need to call the refresh
method: JWTManager.refresh('new-jwt-token')
.
If you would like to decode the JWT token, you can simply use the decode
method: JWTManager.decode()
. This will check to see if there is a token set and if there is, it will decode it and return the decoded object.
When dealing with JWT Tokens it is useful to monitor when the token is close to expiring so a request can be made to generate a new token. JWT manager handles this using a useful monitor
method. For example:
window.JWTManager.monitor((token) => {
// Make request to refresh token here
// Then call the JWTManager.refresh() method and pass in your new token
});
This will then check on an interval basis to see if the JWT Token is going to expire within the next 60 seconds. If it is, it will run the callback. You can also pass a second parameter to the monitor
function. This will set the number of seconds until the token expires, before it triggers the callback. By default it is set to 60. In the below example, it will trigger the callback when the token is due to expire within the next 30 seconds.
window.JWTManager.monitor((token) => {
// Make request to refresh token here
// Then call the JWTManager.refresh() method and pass in your new token
}, 30);
JWT Manager will automatically check in ten second intervals.
You can run the test suite by running npm run test