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

114
Vistas
Validating and comparing req.body values to founduser values

I have an api for reseting password. This api checks if there is a user with the entered phone number and if there is any it checks whether the inputs like dob, nationality and idnumber are also correct then if they are it then generates a new password for the user. However am using many if statements . So my question is whether this is practical or i should change my syntax for performance sake of the app and general good practices for writing standard code.

I have attached my code below with bunch of if statements lol

const asyncHandler = require("express-async-handler");
const User = require("../../models/user")
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");

const generateStrongPassword = require('../../utils/generateStrongPassword');


const resetPassword = asyncHandler(async (req, res) => {
    const { phonenumber, fullname, nationality, nationalidnumber, dateofbirth } = req.body;
    const user = await User.findOne({ phonenumber: phonenumber });

    if (user) {
        if (fullname != user.fullname) {
            throw new Error('incorrect fullname');
        }

        if (nationality != user.nationality) {
            throw new Error('incorrect nationality');
        }

        if (nationalidnumber != user.nationalidnumber) {
            throw new Error('incorrect national id number ');
        }

        if (dateofbirth != user.dateofbirth) {
            throw new Error('incorrect date of birth');
        }

        // if all validations are passed then
        // generate new password
        const newrawpassword = await generateStrongPassword();
        console.log(newrawpassword);
        
        // harsh the password
        const salt = await bcrypt.genSalt(10);
        const newHashedPassword = await bcrypt.hash(newrawpassword, salt);

        const resetPassword = await User.updateOne({ $set: { password: newHashedPassword } })

        res.json('password successfully reseted')

    } else {
        throw new Error('No account has that phone number');
    }
});

module.exports = resetPassword;
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