Wednesday, 26 April 2017

Reset password in node js

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



via Ravi Hooda

No comments:

Post a Comment