I'm implementing a password reset and when clicking on the generated link I'm getting the following error,
Thu Jan 25 2018 16:54:38 GMT+0000 (GMT): GET /users/reset/d590c54753abc449cfcd8aefceb8d519fce4b627
express deprecated res.redirect(url, status): Use res.redirect(status, url) instead routes/users.js:297:9
events.js:136
throw er; // Unhandled 'error' event
^
RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: [object Object]
at ServerResponse.writeHead (_http_server.js:193:11)
at ServerResponse.writeHead (/Users/benbagley/Code/poetry-out-loud/node_modules/on-headers/index.js:55:19)
at ServerResponse._implicitHeader (_http_server.js:184:8)
at write_ (_http_outgoing.js:644:9)
at ServerResponse.write (_http_outgoing.js:629:10)
at writetop (/Users/benbagley/Code/poetry-out-loud/node_modules/express-session/index.js:290:26)
at ServerResponse.end (/Users/benbagley/Code/poetry-out-loud/node_modules/express-session/index.js:338:16)
at ServerResponse.redirect (/Users/benbagley/Code/poetry-out-loud/node_modules/express/lib/response.js:947:10)
at /Users/benbagley/Code/poetry-out-loud/routes/users.js:297:9
at model.Query.<anonymous> (/Users/benbagley/Code/poetry-out-loud/node_modules/mongoose/lib/model.js:4056:16)
at /Users/benbagley/Code/poetry-out-loud/node_modules/kareem/index.js:273:21
at /Users/benbagley/Code/poetry-out-loud/node_modules/kareem/index.js:131:16
at process._tickCallback (internal/process/next_tick.js:150:11)
[nodemon] app crashed - waiting for file changes before starting...
Here are my routes
// Token URL :get
router.get('/users/reset/:token', (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.');
res.redirect('/users/forgot');
}
res.redirect('reset.hbs', {
pageTitle: 'Reset Your Password',
user: req.user
});
});
});
// Token URL :post
router.post('/users/reset/:token', (req, res, next) => {
if(req.body.password === req.body['password-confirm']) {
next();
return;
}
req.flash('error', 'Passwords do not match!');
res.redirect('back');
User.findOne({
resetPasswordToken: req.params.token,
resetPasswordExpires: { $gt: Date.now() }
}, function(err, user) {
if(!user) {
req.flash('error', ' Password reset is invalid or has expired');
res.redirect('/login');
}
const setPassword = promisify(user.setPassword, user);
setPassword(req.body.password);
user.resetPasswordToken = undefined;
user.resetPasswordExpires = undefined;
const updatedUser = user.save();
req.login(updatedUser);
req.flash('success_msg', 'Your password has been reset successfully! You are now logged in!');
res.redirect('/dashboard');
});
});
I think, I'm close to getting this to work, just a few errors I'm running into. This being one of them. I have checked the documentation and I'm not seeing anything mentioning status codes, I believe this is a change in the recent framework, seems quite odd having to define a status code for every redirect or render.
You have to use res.render instead of res.redirect:
res.render('reset.hbs', {
pageTitle: 'Reset Your Password',
user: req.user
});
res.redirect method accept the route string as arguement while res.render accept a string that represent the file path of the view to render