Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

276
Visualizações
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

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

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

about 4 years ago · Santiago Trujillo Relatório

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..');
}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda