@feathersjs/authentication-jwt
JWT authentication strategy for feathers-authentication using Passport
Installation
npm install @feathersjs/authentication-jwt --save
Documentation
API
This module contains 3 core pieces:
- The main entry function
- The
Verifier
class - The
ExtractJwt
object from passport-jwt.
Main Initialization
In most cases initializing the @feathersjs/authentication-jwt
module is as simple as doing this:
app.configure(authentication(settings));
app.configure(jwt());
This will pull from your global auth
object in your config file. It will also mix in the following defaults, which can be customized.
Default Options
{
name: 'jwt',
entity: 'user',
service: 'users',
passReqToCallback: true,
jwtFromRequest: ExtractJwt.fromHeader,
secretOrKey: auth.secret,
session: false
Verifier: Verifier
}
Additional passport-jwt options can be provided.
Verifier
This is the verification class that receives the JWT payload (if verification is successful) and either returns the payload or, if an id
is present in the payload, populates the entity (normally a user
) and returns both the entity and the payload. It has the following methods that can all be overridden. The verify
function has the exact same signature as passport-jwt.
{
constructor(app, options)
verify(req, payload, done)
}
Customizing the Verifier
The Verifier
class can be extended so that you customize it's behavior without having to rewrite and test a totally custom local Passport implementation. Although that is always an option if you don't want use this plugin.
An example of customizing the Verifier:
import jwt, { Verifier } from '@feathersjs/authentication-jwt';
class CustomVerifier extends Verifier {
verify(req, payload, done) {
done(null, payload);
}
}
app.configure(jwt({ Verifier: CustomVerifier }));
This is a collection of functions provided by passport-jwt that allow you to parse the JWT from anywhere. By default the header
field from when you initialize feathers-authentication
is used. However you can customize to pull from whatever you like.
import jwt, { ExtractJwt } from '@feathersjs/authentication-jwt';
app.configure(jwt({ jwtFromRequest: ExtractJwt.fromBodyField('accessToken') }));
Expected Request Data
By default, this strategy expects a payload in this format:
{
strategy: 'jwt',
accessToken: '<token>'
}
Complete Example
Here's a basic example of a Feathers server that uses @feathersjs/authentication-jwt
. You can see a fully working example in the example/ directory.
const feathers = require('feathers');
const rest = require('feathers-rest');
const hooks = require('feathers-hooks');
const memory = require('feathers-memory');
const bodyParser = require('body-parser');
const errorHandler = require('feathers-errors/handler');
const auth = require('feathers-authentication');
const jwt = require('@feathersjs/authentication-jwt');
const app = feathers()
.configure(rest())
.configure(hooks())
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
.configure(auth({ secret: 'super secret' }))
.configure(jwt())
.use('/users', memory())
.use(errorHandler());
app.listen(3030);
console.log('Feathers app started on 127.0.0.1:3030');
License
Copyright (c) 2016
Licensed under the MIT license.