spee.ch/auth/authentication.js
2017-11-03 13:37:23 -07:00

43 lines
1.3 KiB
JavaScript

const db = require('../models');
const logger = require('winston');
module.exports = {
authenticateChannelCredentials (skipAuth, channelName, userPassword) {
return new Promise((resolve, reject) => {
// skip authentication if not needed
if (skipAuth) {
resolve(skipAuth);
return;
}
// authentication
const userName = channelName.substring(1);
logger.debug(`authenticateChannelCredentials > channelName: ${channelName} username: ${userName} pass: ${userPassword}`);
db.User
.findOne({where: { userName }})
.then(user => {
if (!user) {
logger.debug('no user found');
resolve(false);
return;
}
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);
});
})
.catch(error => {
reject(error);
});
});
},
};