wrapped comparePassword in a promise

This commit is contained in:
bill bittner 2017-12-12 10:51:29 -08:00
parent c7ad52216d
commit b7a38dd099

View file

@ -1,6 +1,10 @@
// load dependencies // load dependencies
const logger = require('winston'); const logger = require('winston');
const db = require('./models'); // require our models for syncing const db = require('./models'); // require our models for syncing
// configure logging
const config = require('./config/speechConfig.js');
const logLevel = config.logging.logLevel;
require('./config/loggerConfig.js')(logger, logLevel);
const userName = process.argv[2]; const userName = process.argv[2];
logger.debug('user name:', userName); logger.debug('user name:', userName);
@ -22,20 +26,22 @@ db.sequelize.sync() // sync sequelize
if (!user) { if (!user) {
throw new Error('no user found'); throw new Error('no user found');
} }
return user.comparePassword(oldPassword, (passwordErr, isMatch) => { return new Promise((resolve, reject) => {
user.comparePassword(oldPassword, (passwordErr, isMatch) => {
if (passwordErr) { if (passwordErr) {
throw passwordErr; return reject(passwordErr);
} }
if (!isMatch) { if (!isMatch) {
throw new Error('Incorrect old password.'); return reject('Incorrect old password.');
} }
logger.debug('Password was a match, updating password'); logger.debug('Password was a match, updating password');
return user.changePassword(newPassword); return resolve(user.changePassword(newPassword));
});
}); });
}) })
.then(() => { .then(() => {
logger.debug('Password successfully updated'); logger.debug('Password successfully updated');
}) })
.catch((error) => { .catch((error) => {
logger.error('error:', error); logger.error(error);
}); });