User update

develop
sundowndev 2018-11-17 00:43:58 +01:00
parent 46d54d339f
commit b3b1a79bee
2 changed files with 15 additions and 11 deletions

View File

@ -12,27 +12,22 @@ module.exports = (req, res, next) => {
return next({ status: 401, message: 'User does not exists.' }); return next({ status: 401, message: 'User does not exists.' });
} }
if (req.body.password) { if (req.body.password && req.body.new_password) {
bcrypt.compare(req.body.password, user.password, (error, result) => { bcrypt.compare(req.body.password, user.password, (error, result) => {
if (!result || error) { if (!result || error) {
return next( return next(
{ {
status: 401, status: 401,
message: 'Authentication failed. Wrong password.', message: 'Authentication failed. Wrong password.',
} },
); );
} }
if (req.body.password === req.body.new_password) { return bcrypt.hash(req.body.new_password, null, null, (hashErr, hash) => {
return next( userObj.password = hash;
{
status: 401,
message: 'New password must be different than old password.',
}
);
}
userObj.password = bcrypt.hashSync(req.body.new_password); return userObj.save(() => res.status(200).json(userObj));
});
}); });
} else { } else {
userObj.firstname = req.body.firstname || userObj.firstname; userObj.firstname = req.body.firstname || userObj.firstname;

View File

@ -28,6 +28,15 @@ module.exports = (req, res, next) => {
}); });
} }
/*if (req.body.password && req.body.password === req.body.new_password) {
return next(
{
status: 401,
message: 'New password must be different than old password.',
},
);
}*/
return next(); return next();
}); });
}; };