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-12-01 02:24:48 +01:00
|
|
|
|
|
|
|
function returnUserAndChannelInfo (userInstance) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let userInfo = {};
|
|
|
|
userInfo['id'] = userInstance.id;
|
|
|
|
userInfo['userName'] = userInstance.userName;
|
|
|
|
userInstance
|
2018-02-22 02:02:57 +01:00
|
|
|
.getChannel()
|
|
|
|
.then(({channelName, channelClaimId}) => {
|
|
|
|
userInfo['channelName'] = channelName;
|
|
|
|
userInfo['channelClaimId'] = channelClaimId;
|
|
|
|
return db.Certificate.getShortChannelIdFromLongChannelId(channelClaimId, channelName);
|
|
|
|
})
|
|
|
|
.then(shortChannelId => {
|
|
|
|
userInfo['shortChannelId'] = shortChannelId;
|
|
|
|
resolve(userInfo);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2017-12-01 02:24:48 +01:00
|
|
|
});
|
|
|
|
}
|
2017-09-17 02:50:22 +02:00
|
|
|
|
|
|
|
module.exports = new PassportLocalStrategy(
|
|
|
|
{
|
2017-12-01 00:13:17 +01:00
|
|
|
usernameField: 'username',
|
|
|
|
passwordField: 'password',
|
2017-09-17 02:50:22 +02:00
|
|
|
},
|
2017-12-01 00:13:17 +01:00
|
|
|
(username, password, done) => {
|
2018-03-06 01:37:22 +01:00
|
|
|
return db.User
|
|
|
|
.findOne({
|
|
|
|
where: {userName: username},
|
|
|
|
})
|
2018-02-22 02:02:57 +01:00
|
|
|
.then(user => {
|
|
|
|
if (!user) {
|
2018-03-06 01:37:22 +01:00
|
|
|
logger.debug('no user found');
|
2018-02-22 02:02:57 +01:00
|
|
|
return done(null, false, {message: 'Incorrect username or password'});
|
|
|
|
}
|
2018-03-06 01:37:22 +01:00
|
|
|
return user.comparePassword(password)
|
|
|
|
.then(isMatch => {
|
|
|
|
if (!isMatch) {
|
|
|
|
logger.debug('incorrect password');
|
|
|
|
return done(null, false, {message: 'Incorrect username or password'});
|
|
|
|
}
|
|
|
|
logger.debug('Password was a match, returning User');
|
|
|
|
return returnUserAndChannelInfo(user)
|
|
|
|
.then(userInfo => {
|
|
|
|
return done(null, userInfo);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
return error;
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
return error;
|
|
|
|
});
|
2018-02-22 02:02:57 +01:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
return done(error);
|
|
|
|
});
|
2017-09-18 19:14:06 +02:00
|
|
|
}
|
2017-09-17 02:50:22 +02:00
|
|
|
);
|