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
const logger = require('winston');
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];
logger.debug('user name:', userName);
@ -22,20 +26,22 @@ db.sequelize.sync() // sync sequelize
if (!user) {
throw new Error('no user found');
}
return user.comparePassword(oldPassword, (passwordErr, isMatch) => {
if (passwordErr) {
throw passwordErr;
}
if (!isMatch) {
throw new Error('Incorrect old password.');
}
logger.debug('Password was a match, updating password');
return user.changePassword(newPassword);
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));
});
});
})
.then(() => {
logger.debug('Password successfully updated');
})
.catch((error) => {
logger.error('error:', error);
logger.error(error);
});