Node JS使用Bcrypt.Hash-密码不会在数据库上进行哈希

xienkqul  于 2023-08-04  发布在  Node.js
关注(0)|答案(2)|浏览(95)

我尝试在Node js中使用const bcrypt = require('bcrypt')加密密码;但它不是散列。当你在终端上尝试它的散列,而不是在数据库上,只是发布一个输入的纯文本。
安装和进口
const bcrypt = require('bcrypt');
npm install bcrypt@latest

// Sign-up route
app.post('/signup', async (req, res) => {
  const { fullName, email, password } = req.body;

  try {
    // Hash the password before storing it in the database
    const hashedPassword = await bcrypt.hash(password, 10);

    // Insert user data into the database
    const newUser = { fullName, email, password: hashedPassword };
    db.query('INSERT INTO users SET ?', newUser, (err) => {
      if (err) {
        console.error('Error inserting user:', err);
        res.status(500).json({ error: 'Internal server error' });
        return;
      }

      res.json({ message: 'User registered successfully' });
    });
  } catch (error) {
    console.error('Error hashing password:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
});

字符串
我希望密码被散列,例如密码:123,此$2b$10$8hL4zs.XUIXpc6sxf/kfYeKWAcwduZaMlUsZoRz8.cRc5kz9EzEV2应发布到数据库数据类型密码

oyt4ldly

oyt4ldly1#

感谢所有做出贡献的人,我已经解决了这个问题,我清除了node js缓存,它工作了

npm cache clean --force

字符串

vyswwuz2

vyswwuz22#

希望这对你有帮助。

const bcrypt = require('bcrypt');
const mysql = require('mysql');

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'mydatabase',
});

connection.connect();

app.post('/signup', async (req, res) => {
  const { fullName, email, password } = req.body;

  try {
    // Hash the password before storing it in the database
    const hashedPassword = await bcrypt.hash(password, 10);

    // Insert user data into the database
    const newUser = { fullName, email, password: hashedPassword };
    connection.query('INSERT INTO users SET ?', newUser, (err) => {
      if (err) {
        console.error('Error inserting user:', err);
        res.status(500).json({ error: 'Internal server error' });
        return;
      }

      res.json({ message: 'User registered successfully' });
    });
  } catch (error) {
    console.error('Error hashing password:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
});

connection.end();
const bcrypt = require('bcrypt');
const mysql = require('mysql');

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'mydatabase',
});

connection.connect();

app.post('/signup', async (req, res) => {
  const { fullName, email, password } = req.body;

  try {
    // Hash the password before storing it in the database
    const hashedPassword = await bcrypt.hash(password, 10);

    // Insert user data into the database
    const newUser = { fullName, email, password: hashedPassword };
    connection.query('INSERT INTO users SET ?', newUser, (err) => {
      if (err) {
        console.error('Error inserting user:', err);
        res.status(500).json({ error: 'Internal server error' });
        return;
      }

      res.json({ message: 'User registered successfully' });
    });
  } catch (error) {
    console.error('Error hashing password:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
});

connection.end();

字符串

相关问题