我尝试通过解码在GET请求中作为参数给出的JWT令牌来验证用户电子邮件。
如果令牌有效且用户尚未验证,我想将isVerified列更新为true。
为什么我的承诺总是被拒绝?
const jwt = require('jsonwebtoken');
const request = require('request');
const models = require('../models');
/**
* GET /verify-email/:token
*/
exports.verifyEmail = function(req, res) {
jwt.verify(req.params.token, process.env.TOKEN_SECRET, function(err, decoded) {
if (err) {
res.send({
msg: 'Token is invalid or has expired'
})
}
models.User.findOne({
where: {
email: decoded.email,
id: decoded.id
}
}).then(record => {
if (!record) {
res.send({
msg: 'User not found'
})
} else if (record.isVerified) {
res.send({
msg: 'User already verified'
})
} else {
console.log('user mail ' + record.email + ' will be verified in db')
record.update({
isVerified: true,
})
.then(() => res.status(200).send({
msg: 'User has been verified'
}))
.catch((error) => res.status(400).send(error));
}
})
});
}
答案 0 :(得分:0)
问题解决了,我的模型中有一个beforeValidation钩子:
if (!user.changed('password')) {
return sequelize.Promise.reject("not modified");
}