Local Authentication

GitHub stars
@feathersjs/authentication-local"">npm version
Changelog

  1. $ npm install @feathersjs/authentication-local --save

@feathersjs/authentication-local is a server side module that wraps the passport-local authentication strategy, which lets you authenticate with your Feathers application using a username and password.

This module contains 3 core pieces:

  1. The main initialization function
  2. The hashPassword hook
  3. The Verifier class

Configuration

In most cases initializing the module is as simple as doing this:

  1. const feathers = require('@feathersjs/feathers');
  2. const authentication = require('@feathersjs/authentication');
  3. const local = require('@feathersjs/authentication-local');
  4. const app = feathers();
  5. // Setup authentication
  6. app.configure(authentication(settings));
  7. app.configure(local());
  8. // Setup a hook to only allow valid JWTs or successful
  9. // local auth to authenticate and get new JWT access tokens
  10. app.service('authentication').hooks({
  11. before: {
  12. create: [
  13. authentication.hooks.authenticate(['local', 'jwt'])
  14. ]
  15. }
  16. });

This will pull from your global authentication object in your config file. It will also mix in the following defaults, which can be customized.

Options

  1. {
  2. name: 'local', // the name to use when invoking the authentication Strategy
  3. entity: 'user', // the entity that you're comparing username/password against
  4. service: 'users', // the service to look up the entity
  5. usernameField: 'email', // key name of username field
  6. passwordField: 'password', // key name of password field
  7. entityUsernameField: 'email', // key name of the username field on the entity (defaults to `usernameField`)
  8. entityPasswordField: 'password', // key name of the password on the entity (defaults to `passwordField`)
  9. passReqToCallback: true, // whether the request object should be passed to `verify`
  10. session: false // whether to use sessions,
  11. Verifier: Verifier // A Verifier class. Defaults to the built-in one but can be a custom one. See below for details.
  12. }

Important: When setting the usernameField to username in the configuration the value must be escaped as \\username otherwise it will use the value of the username environment variable on Windows systems.

hooks

hashPassword

This hook is used to hash plain text passwords before they are saved to the database. It uses the bcrypt algorithm by default but can be customized by passing your own options.hash function.

Important: @feathersjs/authentication-local does not allow to store clear text passwords. This means the hashPassword hook must be used when using the standard verifier.

Available options are

  • passwordField (default: 'password') - key name of password field to look on context.data
  • hash (default: bcrypt hash function) - Takes in a password and returns a hash.
  1. const local = require('@feathersjs/authentication-local');
  2. app.service('users').hooks({
  3. before: {
  4. create: [
  5. local.hooks.hashPassword()
  6. ]
  7. }
  8. });

protect

The protect hook makes sure that protected fields don’t get sent to a client.

  1. const local = require('@feathersjs/authentication-local');
  2. app.service('users').hooks({
  3. after: {
  4. create: [
  5. local.hooks.protect('password')
  6. ]
  7. }
  8. });

Verifier

This is the verification class that does the actual username and password verification by looking up the entity (normally a user) on a given service by the usernameField and compares the hashed password using bcrypt. It has the following methods that can all be overridden. All methods return a promise except verify, which has the exact same signature as passport-local.

  1. {
  2. constructor(app, options) // the class constructor
  3. _comparePassword(entity, password) // compares password using bcrypt
  4. _normalizeResult(result) // normalizes result from service to account for pagination
  5. verify(req, username, password, done) // queries the service and calls the other internal functions.
  6. }

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:

  1. import local, { Verifier } from '@feathersjs/authentication-local';
  2. class CustomVerifier extends Verifier {
  3. // The verify function has the exact same inputs and
  4. // return values as a vanilla passport strategy
  5. verify(req, username, password, done) {
  6. // do your custom stuff. You can call internal Verifier methods
  7. // and reference this.app and this.options. This method must be implemented.
  8. // the 'user' variable can be any truthy value
  9. // the 'payload' is the payload for the JWT access token that is generated after successful authentication
  10. done(null, user, payload);
  11. }
  12. }
  13. app.configure(local({ Verifier: CustomVerifier }));

Client Usage

authentication-client

When this module is registered server side, using the default config values this is how you can authenticate using @feathersjs/authentication-client:

  1. app.authenticate({
  2. strategy: 'local',
  3. email: 'your email',
  4. password: 'your password'
  5. }).then(response => {
  6. // You are now authenticated
  7. });

HTTP Request

If you are not using the @feathersjs/authentication-client and you have registered this module server side, make a POST request to /authentication with the following payload:

  1. // POST /authentication the Content-Type header set to application/json
  2. {
  3. "strategy": "local",
  4. "email": "your email",
  5. "password": "your password"
  6. }

Here is what that looks like with curl:

  1. curl -H "Content-Type: application/json" -X POST -d '{"strategy":"local","email":"your email","password":"your password"}' http://localhost:3030/authentication

Sockets

Authenticating using a local strategy via sockets is done by emitting the following message:

  1. const io = require('socket.io-client');
  2. const socket = io('http://localhost:3030');
  3. socket.emit('authenticate', {
  4. strategy: 'local',
  5. email: 'your email',
  6. password: 'your password'
  7. }, function(message, data) {
  8. console.log(message); // message will be null
  9. console.log(data); // data will be {"accessToken": "your token"}
  10. // You can now send authenticated messages to the server
  11. });