Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
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

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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')

over 4 years ago · Santiago Trujillo Report

0

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..');
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!