POST /reset_password
的全部代码
router.post('/reset_password',function(req,res,next){
// From a random token
async.waterfall([
function(done) {
crypto.randomBytes(20, function(err, buf) {
var token = buf.toString('hex');
// works well
console.log(token);
done(err, token);
});
},
function(token, done) {
Account.findOne({ email: req.body.email }, function(err, user) {
if (!user) {
console.log('No email found')
return res.redirect('/reset_password');
}
user.resetPasswordToken = token;
// link will be expired in 20 minutes
user.resetPasswordExpires = Date.now() + 1200000;
user.save(function(err) {
done(err, token, user);
});
});
},
function(token, user, done) {
var transport = nodemailer.createTransport(smtpTransport({
service: 'Gmail',
auth: {
xoauth2: xoauth2.createXOAuth2Generator({
user: 'my_email@gmail.com',
pass: 'my_password'
})
}
}));
var mailOptions = {
to: user.email,
from: 'my_email@gmail.com',
subject: 'Link to reset your password - Behavior Recording',
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.headers.host + '/reset/' + token + '\n\n' +
'If you did not request this, please ignore this email and your password will remain unchanged.\n'
};
transport.sendMail(mailOptions, function(err) {
console.log('An e-mail has been sent to ' + user.email + ' with further instructions.');
res.redirect('/');
done(err, 'done');
});
}
], function(err) {
if (err) return next(err);
console.log('error happend')
res.redirect('reset_password');
})
});
我的代码中存在两个问题:
问题
transport.sendMail(mailOptions, function(err) {
console.log('An e-mail has been sent to ' + user.email + ' with further instructions.');
res.redirect('/');
done(err, 'done');
}
出现500错误:POST /reset_password 500 1849.227 ms - 30
对console.log进行调试,此功能上方的所有代码均有效。
控制台打印An e-mail has been sent to useremail@gmail.com with further instructions.
,res.redirect('/')
存在
因此错误似乎发生在done(err, 'done');
中,但是作为新的node.js用户,我不知道如何在不删除此行的情况下进行修复。
问题b
我已经将我的Google帐户(用于发送电子邮件的帐户)设置为允许安全性较低的应用,但是我无法通过注册用户帐户(用于接收电子邮件的邮件)接收电子邮件
>xoauth2 也在上面的代码中的我的身份验证配置中(这是其他类似的“无法通过Google帐户发送电子邮件”问题的解决方案),但仍然不起作用
user.email存在并且正确