我正在尝试使用This很棒的链接在我的Nodejs应用中编写密码重置代码。
我在这里遇到的问题是,每当我按下reset
时,我都会收到以下错误:
无法将属性'resetPasswordToken'设为null
任何人都可以帮我找出问题吗?以下是我的路线的样子:
app.post('/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) {
res.send ("No user to be found");
}
user.resetPasswordToken = token;
user.resetPasswordExpires = Date.now() + 3600000; // 1 hour
user.save(function(err) {
done(err, token, user);
});
});
},
function(token, user, done) {
var smtpTransport = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'mygamil',
pass: 'my gmail pass'
}
});
var mailOptions = {
to: user.email,
from: 'mygmail',
subject: 'Password Reset',
text: 'You are receiving this because you (or someone else) have requested the reset of the password for your account.\n\n' +
'Please click on the following link, or paste this into your browser to complete the process:\n\n' +
'http://' + req.get('host') + '/reset/' + token + '\n\n' +
'If you did not request this, please ignore this email and your password will remain unchanged.\n'
};
smtpTransport.sendMail(mailOptions, function(err) {
done(err, 'done');
});
}
], function(err) {
if (err) return next(err);
res.redirect('/forgotpassword');
});
});
app.get('/reset/:token', function(req, res) {
User.findOne({ resetPasswordToken: req.params.token, resetPasswordExpires: {
$gt: Date.now() } }, function(err, user) {
if (!user) {
req.flash('error', 'Password reset token is invalid or has expired.');
return res.redirect('/forgotpassword');
}
res.render('reset', {
user: req.user
});
});
});
我错过了什么吗?
我尝试安装crypto
,但收到以下消息:
crypto is not supported and it's now builtin feature!