2017-09-28 19:51:02 +02:00
|
|
|
const db = require('../models');
|
|
|
|
const logger = require('winston');
|
2017-09-15 23:41:47 +02:00
|
|
|
|
2017-09-28 19:51:02 +02:00
|
|
|
module.exports = {
|
2017-10-05 23:48:08 +02:00
|
|
|
authenticateChannelCredentials (channelName, userPassword) {
|
2017-09-28 19:51:02 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-10-05 23:48:08 +02:00
|
|
|
if (!channelName) {
|
2017-09-28 19:51:02 +02:00
|
|
|
resolve(true);
|
|
|
|
return;
|
|
|
|
}
|
2017-10-05 23:48:08 +02:00
|
|
|
const userName = channelName.substring(1);
|
|
|
|
logger.debug(`authenticateChannelCredentials > channelName: ${channelName} username: ${userName} pass: ${userPassword}`);
|
2017-09-28 19:51:02 +02:00
|
|
|
db.User
|
2017-10-05 23:48:08 +02:00
|
|
|
.findOne({where: { userName }})
|
2017-09-28 19:51:02 +02:00
|
|
|
.then(user => {
|
|
|
|
if (!user) {
|
|
|
|
logger.debug('no user found');
|
|
|
|
resolve(false);
|
|
|
|
return;
|
|
|
|
}
|
2017-10-05 23:48:08 +02:00
|
|
|
if (!user.validPassword(userPassword, user.password)) {
|
2017-09-28 19:51:02 +02:00
|
|
|
logger.debug('incorrect password');
|
|
|
|
resolve(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
logger.debug('user found:', user.dataValues);
|
|
|
|
resolve(true);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error(error);
|
|
|
|
reject();
|
|
|
|
});
|
|
|
|
});
|
2017-09-15 23:41:47 +02:00
|
|
|
},
|
|
|
|
};
|