Home Reference Source Repository

src/config/passport.js

// Import the neccesary modules.
import passport from 'passport';
import { Strategy as LocalStrategy } from 'passport-local';

import User from '../models/user';

/**
 * Defining the local strategy.
 * @type {LocalStrategy}
 */
const strategy = new LocalStrategy((username, password, callback) => {
  return User.findOne({ username }).then(user => {
    if (!user) return callback(null, false, {
      message: 'Incorrect username.'
    });

    return user.verifyPassword(password, (err, isMatch) => {
      if (err) return callback(err);
      if (!isMatch) return callback(null, false);
      return callback(null, user);
    });
  }).catch(err => callback(err));
});

passport.use(strategy);