tokenpress
A JWT utility belt for JavaScript applications
Table of Contents
Features
- Convenient, universal utilities for handling JWTs
- JWTs generated by node-jsonwebtoken
- Runs on Node.js v8+
Documentation
Installation
npm install tokenpress
Node.js
Configure tokenpress before using it:
const tokenpress = ; tokenpress;
Sign a token:
const tokenpress = ; const token = tokenpressjwt;
Verify a token using JWKS:
const tokenpress = ; tokenpress; const someToken = 'blah.blah.blah';tokenpressjwt;
Use tokenpress middleware to require authentication for a route:
const tokenpress = ;const requireAuth = tokenpressmiddleware; router;
Note: If the authentication check fails, a 401 (unauthorized) response will be sent as JSON. The response will contain an
error
property that will equal eitherEXPIRED_TOKEN
orINVALID_TOKEN
.INVALID_TOKEN
can be caused by any of the conditions listed in the jsonwebtoken docs.
Generate a random, variable-length, hexadecimal string using the crypto.randomBytes function. The minumum length defaults to 30, and the maximum length defaults to 50.
const tokenpress = ; const randomToken = tokenpressutils;
Browser
Optionally configure whether to use sessionStorage as opposed to localStorage for storing tokens on the client. By default, localStorage will be used.
; tokenpress;
Optionally configure the key used when saving to localStorage or sessionStorage. Defaults to token
.
; tokenpress;
Save a token to localStorage/sessionStorage:
; ;
Retrieve a token from localStorage/sessionStorage:
; const token = tokenpress;
Delete a token from localStorage/sessionStorage:
; tokenpress;
Determine if a token is expired:
; // Will fetch token from localStorage/sessionStorage by defaultconst isTokenExpired = tokenpress;console; // true or false // Or, check a token you've previously retrievedconst token = tokenpress;const isMyOtherTokenExpired = tokenpress;console; // true or false
Contributing
Linting
Run ESLint with npm run lint
.
Testing
Run unit tests with npm test
.