Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

277
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda