After two days of googling I finally post my first question ever:
I try to implement a Forgot password functionality in my NodeJS, Express, website following this : http://sahatyalkabov.com/how-to-implement-password-reset-in-nodejs/
The thing is, in the app.post('reset/:token') route, req.user is undefined, but it's the only place in my code where its undefined !
I think it might come from the "unique link" type of route, but not sure and no idea how to solve this.
The form in the .ejs
action to 'reset/:token' too.
So that resetPasswordToken
can't be find after entering new password ..
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('/forgot');
}
res.render('./pages/reset.ejs', {
username: req.user
});
});
});
app.post('/reset/:token', function(req, res) {
async.waterfall([
function(done) {
console.log('user: ' + req.user);
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('back');
}
user.password = req.body.password;
user.resetPasswordToken = undefined;
user.resetPasswordExpires = undefined;
user.save(function(err) {
req.logIn(user, function(err) {
done(err, user);
});
});
});
},
function(user, done) {
var mailOptions = {
to: user.email,
from: 'passwordreset@demo.com',
subject: 'Your password has been changed',
text: 'Hello,\n\n' +
'This is a confirmation that the password for your account ' + user.email + ' has just been changed.\n'
};
smtpTransport.sendMail(mailOptions, function(err) {
req.flash('success', 'Success! Your password has been changed.');
done(err);
});
}
`enter code here`
], function(err) {
res.redirect('/');
});
});
This is actually my first post here, sorry for bad indentations ..