spee.ch/auth/authentication.js

44 lines
1.3 KiB
JavaScript
Raw Normal View History

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-11-03 21:37:23 +01:00
authenticateChannelCredentials (skipAuth, channelName, userPassword) {
2017-09-28 19:51:02 +02:00
return new Promise((resolve, reject) => {
2017-11-03 21:37:23 +01:00
// skip authentication if not needed
if (skipAuth) {
resolve(skipAuth);
2017-09-28 19:51:02 +02:00
return;
}
2017-11-03 21:37:23 +01:00
// authentication
const userName = channelName.substring(1);
logger.debug(`authenticateChannelCredentials > channelName: ${channelName} username: ${userName} pass: ${userPassword}`);
2017-09-28 19:51:02 +02:00
db.User
.findOne({where: { userName }})
2017-09-28 19:51:02 +02:00
.then(user => {
if (!user) {
logger.debug('no user found');
resolve(false);
return;
}
2017-11-03 21:37:23 +01:00
return user.comparePassword(userPassword, (passwordErr, isMatch) => {
if (passwordErr) {
logger.error('comparePassword error:', passwordErr);
resolve(false);
return;
}
if (!isMatch) {
logger.debug('incorrect password');
resolve(false);
return;
}
logger.debug('...password was a match...');
resolve(true);
});
2017-09-28 19:51:02 +02:00
})
.catch(error => {
2017-11-03 21:37:23 +01:00
reject(error);
2017-09-28 19:51:02 +02:00
});
});
2017-09-15 23:41:47 +02:00
},
};