All files / lib index.js

69.44% Statements 25/36
22.22% Branches 2/9
50% Functions 2/4
71.43% Lines 25/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 67 68 691x 1x 1x 1x 1x 1x 1x   1x   1x 1x 1x   1x   1x       1x   1x   1x   1x   1x 1x   1x   1x         1x   1x                                       1x     1x          
const Debug = require('debug');
const hooks = require('./hooks');
const passport = require('passport');
const adapter = require('./passport');
const getOptions = require('./options');
const createService = require('./service');
const legacySockets = require('./socket');
 
const debug = Debug('@feathersjs/authentication:index');
 
const init = (config = {}) => {
  return app => {
    const _super = app.setup;
    // Merge all options
    const options = getOptions(config, app.get('authentication'));
 
    Iif (app.passport) {
      throw new Error(`You have already registered authentication on this app. You only need to do it once.`);
    }
 
    app.set('authentication', options);
 
    debug('Setting up Passport');
    // Set up our framework adapter
    passport.framework(adapter(options));
    // Expose passport on the app object
    app.passport = passport;
    // Alias to passport for less keystrokes
    app.authenticate = passport.authenticate.bind(passport);
    app.use(options.path, createService(app));
 
    const authService = app.service(options.path);
 
    Iif (typeof authService.publish === 'function') {
      authService.publish(() => false);
    }
    // TODO add service hooks
 
    app.passport.initialize();
 
    app.setup = function () {
      const result = _super.apply(this, arguments);
 
      // Socket.io middleware
      if (app.io) {
        debug('registering Socket.io authentication middleware');
        app.io.on('connection', legacySockets(app, options));
      }
 
      // Primus middleware
      if (app.primus) {
        debug('registering Primus authentication middleware');
        app.primus.on('connection', legacySockets(app, options));
      }
 
      return result;
    };
  };
};
 
module.exports = init;
 
// Exposed Modules
Object.assign(module.exports, {
  default: init,
  hooks,
  createService
});