fix: 更改密码修改逻辑

This commit is contained in:
chaosBreaking
2019-09-06 23:25:58 +08:00
parent 8724afbeb7
commit 058c0ac832

View File

@@ -200,32 +200,28 @@ module.exports = {
changePasswd: inject(function (SysConfig) {
const { PWD_HASH_SALT } = SysConfig;
return async (req, res) => {
const { phone, password, code } = req.body;
const validCode = await Code.verifyCode({
id: phone,
code,
type: CODE_TYPE['changePasswd']
});
if (!validCode) {
res.json({
const { userId } = res.locale.user;
const { password, newPassword } = req.body;
const user = User.findById(userId);
if (!user || user.password !== Crypto.hash(password, { salt: PWD_HASH_SALT }))
return res.json({
code: 1,
msg: 'Incorrect code'
msg: '原始密码错误'
});
}
const newPasswd = Crypto.hash(password, PWD_HASH_SALT);
const query = { phone };
const update = { password: newPasswd };
const newHashPasswd = Crypto.hash(newPassword, PWD_HASH_SALT);
const update = { password: newHashPasswd };
const success = await User.findByIdAndUpdate(userId, update, { new: true })
// eslint-disable-next-line no-unused-vars
const success = await User.findOneAndUpdate(query, update, { new: true }).exec().catch(err => null);
.exec().catch(err => null);
if (success) {
res.json({
code: 0,
msg: 'Reseted passwd'
msg: '密码重置成功'
});
} else {
res.json({
code: 500,
msg: 'unknow error'
msg: '系统错误'
});
}
};