All files / src/server/middleware authentication.middleware.js

100% Statements 9/9
100% Branches 7/7
100% Functions 1/1
100% Lines 9/9

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 236x 6x             6x   732x 726x       6x 5x 5x   1x      
const noOpMiddleware = require('./noop.middleware.js');
const passport = require('passport');
 
/**
 * @description Middleware for doing authentication, simople wrapper around passport
 * @param {Object} config - Configurations for the application
 * @return {function} valid express middleware
 */
module.exports = function authenticationMiddleware(config) {
  // Don't do any validation for testing
  if (process.env.NODE_ENV === 'test') {
    return noOpMiddleware;
  }
 
  // if strategy is configured
  if (config.auth && config.auth.strategy) {
    let { name, useSession = false } = config.auth.strategy;
    return passport.authenticate(name, { session: useSession });
  } else {
    return noOpMiddleware;
  }
};