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

188
Vistas
hashed password not works with me

I don't know why this hash password code does not work. I did install bcrypt, also, it should go to the line (res.send("testing"))if the passwords are the same but anyway in all situations password does not match is coming even they are the same.

Here is my code:

const mysql = require('mysql');
const jwt = require('jsonwebtoken');
const bcrypt = require('bcryptjs');
const db = mysql.createConnection({
  host: process.env.DATABASE_host,
  user: process.env.DATABASE_user,
  password: process.env.DATABASE_password,
  database: process.env.DATABASE,
});

exports.form = (req, res) => {
  console.log(req.body);

  const { name, email, password, confirmPassword } = req.body;
  db.query(
    'SELECT email FROM users WHERE email=?',
    [email],
    async (error, results) => {
      if (error) {
        console.log(error);
      }

      if (results.length > 0) {
        return res.render('form', {
          message: 'that email is already in use',
        });
      } else if (password !== confirmPassword) {
        return res.render('form', {
          message: 'passwords not match',
        });
      }

      let hashedPassword = await bcrypt.hash('password', 8);
      console.log(hashedPassword);
      res.send('testing');
    }
  );
};
``

[enter image description here][1]


  [1]: https://i.stack.imgur.com/ToNvN.png

and always (passwords not match) comes even as u see in pic the passwords are same 
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Every time you call bcrypt.hash() you will get a different hash string, even with the same password, this is because the hashes are salted.

To check whether the hashes are equal, you need to test with bcrypt.compare(), you cannot compare to hashes directly. Some libraries also call it bcrypt.verify().

Edit: Assuming you use the node.bcrypt.js library:

const bcrypt = require('bcrypt');

// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
let hashToStoreInDb = bcrypt.hashSync('mypassword', 10);

// Check if the entered login password matches the stored hash.
// The salt and the cost factor will be extracted from existingHashFromDb.
let existingHashFromDb = hashToStoreInDb;
const isPasswordCorrect = bcrypt.compareSync('mypassword', existingHashFromDb);
about 4 years ago · Juan Pablo Isaza 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