spee.ch/passport/local-login.js

48 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-09-17 02:50:22 +02:00
const PassportLocalStrategy = require('passport-local').Strategy;
2017-09-18 19:14:06 +02:00
const db = require('../models');
const logger = require('winston');
2017-11-30 23:46:32 +01:00
const { returnUserAndChannelInfo } = require('../helpers/authHelpers.js');
2017-09-17 02:50:22 +02:00
module.exports = new PassportLocalStrategy(
{
usernameField : 'username', // username key in the request body
passwordField : 'password', // password key in the request body
session : false,
passReqToCallback: true,
},
2017-09-18 19:14:06 +02:00
(req, username, password, done) => {
2017-11-30 23:46:32 +01:00
logger.debug('logging user in');
return db
.User
2017-09-26 06:03:43 +02:00
.findOne({where: {userName: username}})
2017-09-17 02:50:22 +02:00
.then(user => {
if (!user) {
2017-11-30 23:46:32 +01:00
// logger.debug('no user found');
2017-09-17 02:50:22 +02:00
return done(null, false, {message: 'Incorrect username or password.'});
}
return user.comparePassword(password, (passwordErr, isMatch) => {
if (passwordErr) {
logger.error('passwordErr:', passwordErr);
2017-11-30 23:46:32 +01:00
return done(null, false, {message: passwordErr});
}
if (!isMatch) {
2017-11-30 23:46:32 +01:00
// logger.debug('incorrect password');
return done(null, false, {message: 'Incorrect username or password.'});
}
2017-11-30 23:46:32 +01:00
logger.debug('Password was a match, returning User');
return returnUserAndChannelInfo(user)
.then((userInfo) => {
return done(null, userInfo);
})
.catch(error => {
2017-11-30 23:46:32 +01:00
return done(error);
});
});
2017-09-17 02:50:22 +02:00
})
.catch(error => {
return done(error);
});
2017-09-18 19:14:06 +02:00
}
2017-09-17 02:50:22 +02:00
);