Im trying to add reset password feature.User enters username,email and full name,when these values match data from database ,password is changed,otherwise an error is shown. This is what im doing-
app.post("/reset",function(req,res){
User.findByUsername(req.body.username).then(function(sanitizedUser){
if (sanitizedUser){
sanitizedUser.setPassword(req.body.password, function(){
sanitizedUser.save();
req.flash("success","password resetted");
res.redirect("/login");
});
} else {
req.flash("error","User doesnt exist");
res.redirect("/reset");
}
},function(err){
console.log(err);res.redirect("/");
});
});
But i want to compare more than just the username,i want to compare email and name of the user too.And when i add-
User.find({username:req.body.username},{email:req.body.email},{name:req.body.name})and enter some wrong data,the page just keeps reloading rather than showing an error.
What changes should i make to do that?Please help.
Im using express,nodejs,mongodb
You have a redirect loop.
In the case of !sanitizedUser you redirect to the same page res.redirect('/reset').
This is causing your page to keep reloading.
You should change the line:
res.redirect('/reset')
to
res.status(500).send('Some useful error message')
if(req.body.username && req.body.email && req.body.name && req.body.password){
//add logic to hash password below
let hashedPassword = req.body.password;
Model.findOneAndUpdate(
{ username : req.body.username, email : req.body.email, name : req.body.name },
{ "$set" : { password : hashedPassword } },
//if below option "new" is set to true, call back will return new document else it will return old document beforeupdate
{ new : true },
//call back
function(err, person){
if(err){
//err found
res.send(err);
}
else{
// no error and also person exists
res.send('Password successfully updated..');
}
}
);
}
else{
res.send('Please provide all details..');
}