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

176
Vistas
Bcrypt password reset not taking but rest of function works

I'm on the final step of my password reset workflow and am experiencing a weird issue where the code runs successfully, prints out my artist document as well as the words "Successfully resubmitted password".

Then, without stopping, runs a second time and prints out null for artist and flashes error "Password reset token is invalid or has expired".

Confusing part The new password doesn't take but the variables resetPasswordToken and resetPasswordExpires are both set to null, just like the success part of the if statement requests.

Reset password function

const bcrypt = require('bcrypt');

module.exports.submitNewPassword = async (req, res) => {
    const slidedHeaderToken = req.headers.referer.slice(-40);
    const artist = await Artist.findOne({ resetPasswordToken: slidedHeaderToken, resetPasswordExpires: { $gt: Date.now() } });
    console.log(artist);
    if (!artist) {
        req.flash('error', "Password reset token is invalid or has expired");
        res.render('artists/reset')
    } else {
        const hashedPassword = bcrypt.hashSync(req.body.password, 12)
        artist.password = hashedPassword;
        artist.resetPasswordToken = null;
        artist.resetPasswordExpires = null;
        artist.save();
        console.log("Successfully resubmitted password");
        req.flash('success', "Successfully resubmitted password, please login with your updated credentials");
        res.redirect('login');
    }
}

Route

const passport = require('passport');

router.route('/reset/:token')
    .get( artists.renderPasswordReset)
    .post( catchAsync (artists.submitNewPassword ))

In my app, artists are users but not sure if I need to clarify that somewhere for passport or bcrypt.

Any ideas what's going on with my code and why 2/3 of it works?

about 4 years ago · Juan Pablo Isaza
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