From c7ad52216dd31c0ba948e28768b292dd79405ec4 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 12 Dec 2017 09:21:20 -0800 Subject: [PATCH] added update-passwords script --- update-password.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 update-password.js diff --git a/update-password.js b/update-password.js new file mode 100644 index 00000000..5b94e93c --- /dev/null +++ b/update-password.js @@ -0,0 +1,41 @@ +// load dependencies +const logger = require('winston'); +const db = require('./models'); // require our models for syncing + +const userName = process.argv[2]; +logger.debug('user name:', userName); +const oldPassword = process.argv[3]; +logger.debug('old password:', oldPassword); +const newPassword = process.argv[4]; +logger.debug('new password:', newPassword); + +db.sequelize.sync() // sync sequelize + .then(() => { + logger.info('finding user profile'); + return db.User.findOne({ + where: { + userName: userName, + }, + }); + }) + .then(user => { + 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); + }); + }) + .then(() => { + logger.debug('Password successfully updated'); + }) + .catch((error) => { + logger.error('error:', error); + });