made bcrypt password check into a promise

This commit is contained in:
bill bittner 2018-03-05 16:37:22 -08:00
parent cc3fa3d067
commit 8fbd40370f
5 changed files with 47 additions and 49 deletions

View file

@ -56,20 +56,18 @@ module.exports = {
logger.debug('no user found'); logger.debug('no user found');
throw new Error('Authentication failed, you do not have access to that channel'); throw new Error('Authentication failed, you do not have access to that channel');
} }
return user.comparePassword(userPassword, (passwordErr, isMatch) => { return user.comparePassword(userPassword);
if (passwordErr) { })
logger.error('comparePassword error:', passwordErr); .then(isMatch => {
throw new Error('Authentication failed, you do not have access to that channel');
}
if (!isMatch) { if (!isMatch) {
logger.debug('incorrect password'); logger.debug('incorrect password');
throw new Error('Authentication failed, you do not have access to that channel'); throw new Error('Authentication failed, you do not have access to that channel');
} }
logger.debug('...password was a match...'); logger.debug('...password was a match...');
resolve(channelData); resolve(channelData);
});
}) })
.catch(error => { .catch(error => {
logger.warn('authenticateChannelCredentials error');
reject(error); reject(error);
}); });
}); });

View file

@ -24,8 +24,8 @@ module.exports = (sequelize, { STRING }) => {
User.hasOne(db.Channel); User.hasOne(db.Channel);
}; };
User.prototype.comparePassword = function (password, callback) { User.prototype.comparePassword = function (password) {
bcrypt.compare(password, this.password, callback); return bcrypt.compare(password, this.password);
}; };
User.prototype.changePassword = function (newPassword) { User.prototype.changePassword = function (newPassword) {

View file

@ -30,32 +30,32 @@ module.exports = new PassportLocalStrategy(
passwordField: 'password', passwordField: 'password',
}, },
(username, password, done) => { (username, password, done) => {
logger.debug('logging user in'); return db.User
return db .findOne({
.User where: {userName: username},
.findOne({where: {userName: username}}) })
.then(user => { .then(user => {
if (!user) { if (!user) {
// logger.debug('no user found'); logger.debug('no user found');
return done(null, false, {message: 'Incorrect username or password'}); return done(null, false, {message: 'Incorrect username or password'});
} }
user.comparePassword(password, (passwordErr, isMatch) => { return user.comparePassword(password)
if (passwordErr) { .then(isMatch => {
logger.error('passwordErr:', passwordErr);
return done(null, false, {message: passwordErr});
}
if (!isMatch) { if (!isMatch) {
// logger.debug('incorrect password'); logger.debug('incorrect password');
return done(null, false, {message: 'Incorrect username or password'}); return done(null, false, {message: 'Incorrect username or password'});
} }
logger.debug('Password was a match, returning User'); logger.debug('Password was a match, returning User');
return returnUserAndChannelInfo(user) return returnUserAndChannelInfo(user)
.then((userInfo) => { .then(userInfo => {
return done(null, userInfo); return done(null, userInfo);
}) })
.catch(error => { .catch(error => {
return done(error); return error;
}); });
})
.catch(error => {
return error;
}); });
}) })
.catch(error => { .catch(error => {

View file

@ -178,6 +178,7 @@ module.exports = (app) => {
} }
}) })
.catch(error => { .catch(error => {
console.log('handling error response...');
errorHandlers.handleErrorResponse(originalUrl, ip, error, res); errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
}); });
}); });

View file

@ -26,18 +26,17 @@ db.sequelize.sync() // sync sequelize
if (!user) { if (!user) {
throw new Error('no user found'); throw new Error('no user found');
} }
return new Promise((resolve, reject) => { return Promise.all([
user.comparePassword(oldPassword, (passwordErr, isMatch) => { user.comparePassword(oldPassword),
if (passwordErr) { user,
return reject(passwordErr); ]);
} })
.then(([isMatch, user]) => {
if (!isMatch) { if (!isMatch) {
return reject('Incorrect old password.'); throw new Error('Incorrect old password.');
} }
logger.debug('Password was a match, updating password'); logger.debug('Password was a match, updating password');
return resolve(user.changePassword(newPassword)); return user.changePassword(newPassword);
});
});
}) })
.then(() => { .then(() => {
logger.debug('Password successfully updated'); logger.debug('Password successfully updated');