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');
throw new Error('Authentication failed, you do not have access to that channel');
}
return user.comparePassword(userPassword, (passwordErr, isMatch) => {
if (passwordErr) {
logger.error('comparePassword error:', passwordErr);
throw new Error('Authentication failed, you do not have access to that channel');
}
if (!isMatch) {
logger.debug('incorrect password');
throw new Error('Authentication failed, you do not have access to that channel');
}
logger.debug('...password was a match...');
resolve(channelData);
});
return user.comparePassword(userPassword);
})
.then(isMatch => {
if (!isMatch) {
logger.debug('incorrect password');
throw new Error('Authentication failed, you do not have access to that channel');
}
logger.debug('...password was a match...');
resolve(channelData);
})
.catch(error => {
logger.warn('authenticateChannelCredentials error');
reject(error);
});
});

View file

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

View file

@ -30,33 +30,33 @@ module.exports = new PassportLocalStrategy(
passwordField: 'password',
},
(username, password, done) => {
logger.debug('logging user in');
return db
.User
.findOne({where: {userName: username}})
return db.User
.findOne({
where: {userName: username},
})
.then(user => {
if (!user) {
// logger.debug('no user found');
logger.debug('no user found');
return done(null, false, {message: 'Incorrect username or password'});
}
user.comparePassword(password, (passwordErr, isMatch) => {
if (passwordErr) {
logger.error('passwordErr:', passwordErr);
return done(null, false, {message: passwordErr});
}
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 done(error);
});
});
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;
});
})
.catch(error => {
return done(error);

View file

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

View file

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