const debug = require('debug')('crowi:service:PassportService'); const passport = require('passport'); const LocalStrategy = require('passport-local').Strategy; /** * the service class of Passport */ class PassportService { // see '/lib/form/login.js' static get USERNAME_FIELD() { return 'loginForm[username]' } static get PASSWORD_FIELD() { return 'loginForm[password]' } constructor(crowi) { this.crowi = crowi; } /** * setup LocalStrategy * * @memberof PassportService */ setupLocalStrategy() { debug('setup LocalStrategy'); const User = this.crowi.model('User'); passport.use(new LocalStrategy( { usernameField: PassportService.USERNAME_FIELD, passwordField: PassportService.PASSWORD_FIELD, }, (username, password, done) => { // find user User.findUserByUsernameOrEmail(username, password, (err, user) => { if (err) { return done(err); } // check existence and password if (!user || !user.isPasswordValid(password)) { return done(null, false, { message: 'Incorrect credentials.' }); } return done(null, user); }); } )); } /** * setup serializer and deserializer * * @memberof PassportService */ setupSerializer() { debug('setup serializer and deserializer'); const User = this.crowi.model('User'); passport.serializeUser(function(user, done) { done(null, user.id); }); passport.deserializeUser(function(id, done) { User.findById(id, function(err, user) { done(err, user); }); }); } } module.exports = PassportService;