diff --git a/auth/authentication.js b/auth/authentication.js index 4ab54673..4e8eccf0 100644 --- a/auth/authentication.js +++ b/auth/authentication.js @@ -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); }); }); diff --git a/models/user.js b/models/user.js index 55f75114..ed3de074 100644 --- a/models/user.js +++ b/models/user.js @@ -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) { diff --git a/passport/local-login.js b/passport/local-login.js index 99e9fc50..89b5a968 100644 --- a/passport/local-login.js +++ b/passport/local-login.js @@ -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); diff --git a/routes/api-routes.js b/routes/api-routes.js index 1182ef72..0097da7a 100644 --- a/routes/api-routes.js +++ b/routes/api-routes.js @@ -178,6 +178,7 @@ module.exports = (app) => { } }) .catch(error => { + console.log('handling error response...'); errorHandlers.handleErrorResponse(originalUrl, ip, error, res); }); }); diff --git a/task-scripts/update-password.js b/task-scripts/update-password.js index 89cff11f..e5bfb7d5 100644 --- a/task-scripts/update-password.js +++ b/task-scripts/update-password.js @@ -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');