No sé por qué este código de contraseña hash no funciona. bcrypt , también, debería ir a la línea ( res.send("testing") ) si las contraseñas son las mismas, pero de todos modos, en todas las situaciones, la contraseña no coincide, incluso si son las mismas.
Aquí está mi código:
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 sameCada vez que llame a bcrypt.hash() obtendrá una cadena hash diferente, incluso con la misma contraseña, esto se debe a que los hash están salteados.
Para verificar si los hashes son iguales, debe probar con bcrypt.compare() , no puede comparar los hashes directamente. Algunas bibliotecas también lo llaman bcrypt.verify() .
Editar: suponiendo que use la biblioteca node.bcrypt.js :
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);