spee.ch/passport/local-login.js

49 lines
1.7 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-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-09-21 00:43:42 +02:00
logger.debug(`verifying loggin attempt ${username} ${password}`);
let userInfo = {};
2017-09-17 02:50:22 +02:00
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-09-21 00:43:42 +02:00
logger.debug('no user found');
2017-09-17 02:50:22 +02:00
return done(null, false, {message: 'Incorrect username or password.'});
}
2017-09-18 19:14:06 +02:00
if (!user.validPassword(password, user.password)) {
2017-09-21 00:43:42 +02:00
logger.debug('incorrect password');
2017-09-17 02:50:22 +02:00
return done(null, false, {message: 'Incorrect username or password.'});
}
2017-09-21 00:43:42 +02:00
logger.debug('user found:', user.dataValues);
userInfo['id'] = user.id;
userInfo['userName'] = user.userName;
2017-10-12 23:37:25 +02:00
// channel stuff
return user.getChannel()
.then(channel => {
userInfo['channelName'] = channel.channelName;
userInfo['channelClaimId'] = channel.channelClaimId;
return db.getShortChannelIdFromLongChannelId(channel.channelClaimId, channel.channelName);
})
.then(shortChannelId => {
userInfo['shortChannelId'] = shortChannelId;
return done(null, userInfo);
})
.catch(error => {
throw 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
);