nickschot / lux-jwt

Middleware implementation of JWT for Lux.
MIT License
15 stars 1 forks source link
authentication jwt lux lux-middleware nodejs

lux-jwt

Middleware implementation of JWT for Lux.

Build Status Coverage Status Dependency Status npm version

This module lets you authenticate HTTP requests using JWT tokens in your Lux applications. JWTs are typically used to protect (stateless) API endpoints.

Install

$ npm i --save lux-jwt

Usage

The JWT authentication middleware authenticates callers using a JWT. If the token is valid, request.user will be set with the JSON object decoded to be used by later middleware for authorization and access control.

An example usage of using lux-jwt is shown below.

Secret can also be an Array of multiple valid secrets. A good use case for this is when you use automatically refreshed secrets. This way the previous secret is still valid so the token isn't immediately invalidated when the secret is refreshed. See Heroku Secure Key for more information.

import {Controller} from 'lux-framework';
import jwt from 'lux-jwt';
import unless from 'lux-unless';

class ApplicationController extends Controller {
    beforeAction = [
        jwt({secret: 'shhhhhhared-secret'})
    ];
}

lux-unless can be used to keep certain endpoints from being authorized by lux-jwt.

import {Controller} from 'lux-framework';
import jwt from 'lux-jwt';
import unless from 'lux-unless';

class ApplicationController extends Controller {
    beforeAction = [
        unless({path: ['/users/login']}, jwt({secret: 'shhhhhhared-secret'}))
    ];
}

This module also exposes the jsonwebtoken API. Currently this means the following functions are exposed:

Options

An object containing the following options must be passed:

Related Modules

Tests

$ npm install
$ npm test

License

This project is licensed under the MIT license. See the LICENSE file for more info.