I am trying to change the text of a route with a dynamic parameter depending on what value is passed to the parameter. There are only two cases where a value can appear, request and confirmation and I am trying to figure out the best way to return values to one variable for use within my view based on what value appears. I would like to use resetType as the object for my view, rather than both resetRequest and resetConfirmation and be able to use as an If statement with my view templating system, handlebars. My current solution is definitely not the best and I keep getting the request portion of my if statement returned regardless of the parameter. Any advice to clean up my logic?
Current Logic:
/*==== /FORGOT-PASSWORD/CONFIRMATION ====*/
siteRoutes.route('/password-reset/type/:response')
.get(function(req, res){
var resetRequest;
var resetConfirmation;
if (req.params.response = "request"){
resetRequest = true;
console.log("Request Param");
console.log("Request is set to " + resetRequest);
} else if (req.params.response = "confirmation"){
resetConfirmation = true;
console.log("Confirmation Param");
console.log("Confirmation is set to " + resetConfirmation);
} else {
return null;
console.log("Null");
}
res.render('pages/site/password-reset-confirmation.hbs', {
message: req.flash('info'),
resetRequest: resetRequest,
resetConfirmation: resetConfirmation
});
});
Request Param being passed on POST:
/*==== /FORGOT-PASSWORD ====*/
siteRoutes.route('/password-reset')
.get(function(req, res){
res.render('pages/site/forgot-password.hbs');
})
.post(function(req, res){
req.flash('info', 'Please check your email for the reset link.');
res.redirect('/password-reset/type/request');
})], function(error){
if(error){
console.log(error);
}
})
});
Confirmation Param being passed on POST:
/*==== /RESET ====*/
siteRoutes.route('/password-reset/:token')
.get(function(req, res){
var urlPath = url.parse(req.url).pathname.split('/');
var urlToken = urlPath[2];
console.log(urlToken);
models.User.findOne({
where: {
resetPasswordToken: req.params.token,
resetPasswordExpires: {
$gt: moment().format('YYYY-MM-DD HH:mm:ss')
}
}
}).then(function(user){
res.render('pages/app/password-reset.hbs',{
urlToken: urlToken,
user: user
});
})
})
.post(function(req, res){
async.waterfall([
function(done){
models.User.update({
password: models.User.generateHash(req.body.password),
resetPasswordToken: null,
resetPasswordExpires: null
}, { where: {
resetPasswordToken: req.body.token,
resetPasswordExpires: {
$gt: moment().format('YYYY-MM-DD HH:mm:ss')
}
}})
// Nodemailer
var transporter = nodemailer.createTransport(sgTransport(options));
var mailOptions = {
from: '"Test@test.com',
to: req.body.email,
subject: 'Your Password has Been Changed',
text: 'Hello,\n\n' +
'This is a confirmation that the password for the email associated with your Test account, ' + req.body.email + ' has just been changed. If there has been a mistake or you did not change the password, then please contact us as soon as possible at support@test.com\n'
};
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error + 'During Post');
}
console.log('Message sent: ' + info);
})
}
])
req.flash('info', 'You have successfully updated your password.');
res.redirect('/password-reset/type/confirmation');
});
Aucun commentaire:
Enregistrer un commentaire