我創建爲我的應用忘記密碼的功能,現在,我掙扎着一個關鍵的步驟添加到兩個字段是將數據添加兩個當用戶開始忘記密碼處理時,忘記密碼相關的字段。如何將數據在收集一行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');
});
},