我创建为我的应用忘记密码的功能,现在,我挣扎着一个关键的步骤添加到两个字段是将数据添加两个当用户开始忘记密码处理时,忘记密码相关的字段。如何将数据在收集一行SailsJS水线update()方法
的字段是
1)resetPasswordToken
- 甲随机生成的令牌,将暂时允许用户设置新密码。
2)resetPasswordExpires
- 从创作整整一个小时一个日期组创建的密码是多久可以改变一个时间限制。
我得到的地方,我需要更新的用户行的一部分(需要用户更改密码!)与这两个领域,我不知道该怎么做。我正尝试使用SailsJS Update method来完成此操作。以前当我使用update()
方法被更新的款式,我可以做到这一点使用一些标准的符号,像这样:
User.update(req.param('id'), req.body)
.exec(function (err, users) {
if (err) { return res.json(400, err); }
res.json(users[0]);
});
这是很容易,因为我被同时更新整个行,和2由于那我有id
我可以通过。更新整行是使用我已发现有较好文档记录的更新的唯一方法。
不过,现在困难得多。我需要更新两个字段,并且我不认为我在执行此服务器端操作时可以使用id
。文档没有指定如何做这样的事情。我有他们用于注册,这是潮头的email
地址,所以它应该是可能的。我需要使用某种我认为的.where()
?
此时整个服务器端的操作看起来有点像这样:
forgot: function (req, res, next) {
async.waterfall([
function(done) {
crypto.randomBytes(20, function(err, buf) {
var token = buf.toString('hex');
done(err, token);
});
},
function(token, done) {
User.findOne({ email: req.body.email }, function(err, user) {
if (!user) {
done(null, false, { message: 'Email doesn't exist' });
}
user.resetPasswordToken = token;
user.resetPasswordExpires = Date.now() + 3600000;
// TODO: this part will have to be figured out
// Have no idea how to update correctly! Left it blank
User.update();
// Above is the key problem
done(err, token, user);
});
},
function(token, user, done) {
htmlMessage = '<h4>You are receiving this because you (or someone else) have requested the reset of the password for your account.</h4>' +
'<p>Please click on the following link, or paste this into your browser to complete the process:</p>' +
"<p><a href='http://" + req.headers.host + '/reset/' + token + "'> Link to password reset </a></p>" +
'<p>If you did not request this, please ignore this email and your password will remain unchanged.</p>';
var emailInfo = {
to: user.email,
from: "[email protected]",
subject: "Password Reset",
message: htmlMessage,
fromName: "Random Organization"
};
EmailService.simpleSendEmail(emailInfo, function (err) {
done(err, 'done');
});
}
], function(err) {
if (err) return next(err);
res.redirect('/login');
});
},