fixed a few bugs

This commit is contained in:
bill bittner 2018-05-31 09:36:45 -07:00
parent 7b644f9991
commit b4fb633c23

View file

@ -1,9 +1,7 @@
const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
const logger = require('winston'); const logger = require('winston');
const db = require('../../../../models'); const db = require('../../../../models');
const siteConfig = require('./config/siteConfig.js'); const { auth: { masterPassword } } = require('../../../../../config/siteConfig.js');
const masterPassword = siteConfig.auth.sessionKey;
/* /*
@ -14,9 +12,20 @@ const masterPassword = siteConfig.auth.sessionKey;
const updateUserPassword = ({ ip, originalUrl, body }, res) => { const updateUserPassword = ({ ip, originalUrl, body }, res) => {
let userRecord; let userRecord;
const { userName, oldPassword, newPassword } = body; const { userName, oldPassword, newPassword } = body;
if (!user || !oldPassword || newPassword) { logger.info('body:', body);
return res.status(400).json({success: false, message: 'body should include userName (channel name without the @), oldPassword, & newPassword'}); if (!masterPassword) {
}; return res.status(400).json({
success: false,
message: 'no master password set in site config',
});
}
if (!userName || !oldPassword || !newPassword) {
return res.status(400).json({
success: false,
message: 'body should include userName (channel name without the @), oldPassword, & newPassword',
});
}
db.User.findOne({ db.User.findOne({
where: { where: {
@ -24,16 +33,16 @@ const updateUserPassword = ({ ip, originalUrl, body }, res) => {
}, },
}) })
.then(user => { .then(user => {
if (!user) { userRecord = user;
if (!userRecord) {
throw new Error('no user found'); throw new Error('no user found');
} }
userRecord = user;
if (oldPassword === masterPassword) { if (oldPassword === masterPassword) {
console.log('master password provided'); console.log('master password provided');
return true; return true;
} else { } else {
console.log('old password provided'); console.log('old password provided');
return user.comparePassword(oldPassword); return userRecord.comparePassword(oldPassword);
} }
}) })
.then(isMatch => { .then(isMatch => {
@ -41,7 +50,7 @@ const updateUserPassword = ({ ip, originalUrl, body }, res) => {
throw new Error('Incorrect old password.'); throw new Error('Incorrect old password.');
} }
logger.debug('Password was a match, updating password'); logger.debug('Password was a match, updating password');
return user.changePassword(newPassword); return userRecord.changePassword(newPassword);
}) })
.then(() => { .then(() => {
logger.debug('Password successfully updated'); logger.debug('Password successfully updated');