All files / src index.js

100% Statements 33/33
93.75% Branches 15/16
100% Functions 3/3
100% Lines 33/33
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 671x 1x 1x 1x 1x 1x 1x   1x 1x           1x             16x 16x 16x 16x   16x 1x     15x 15x 15x     15x 15x   15x 2x     15x 15x 15x   15x 1x       14x 14x 14x   14x           1x        
import Debug from 'debug';
import merge from 'lodash.merge';
import omit from 'lodash.omit';
import pick from 'lodash.pick';
import hooks from './hooks';
import DefaultVerifier from './verifier';
import { Strategy as LocalStrategy } from 'passport-local';
 
const debug = Debug('feathers-authentication-local');
const defaults = {
  name: 'local',
  usernameField: 'email',
  passwordField: 'password'
};
 
const KEYS = [
  'entity',
  'service',
  'passReqToCallback',
  'session'
];
 
export default function init(options = {}) {
  return function localAuth() {
    const app = this;
    const _super = app.setup;
 
    if (!app.passport) {
      throw new Error(`Can not find app.passport. Did you initialize feathers-authentication before feathers-authentication-local?`);
    }
 
    let name = options.name || defaults.name;
    let authOptions = app.get('auth') || {};
    let localOptions = authOptions[name] || {};
 
    // NOTE (EK): Pull from global auth config to support legacy auth for an easier transition.
    const localSettings = merge({}, defaults, pick(authOptions, KEYS), localOptions, omit(options, ['Verifier']));
    let Verifier = DefaultVerifier;
 
    if (options.Verifier) {
      Verifier = options.Verifier;
    }
 
    app.setup = function () {
      let result = _super.apply(this, arguments);
      let verifier = new Verifier(app, localSettings);
 
      if (!verifier.verify) {
        throw new Error(`Your verifier must implement a 'verify' function. It should have the same signature as a local passport verify callback.`)
      }
 
      // Register 'local' strategy with passport
      debug('Registering local authentication strategy with options:', localSettings);
      app.passport.use(localSettings.name, new LocalStrategy(localSettings, verifier.verify.bind(verifier)));
      app.passport.options(localSettings.name, localSettings);
      
      return result;
    }
  };
}
 
// Exposed Modules
Object.assign(init, {
  defaults,
  hooks,
  Verifier: DefaultVerifier
});