skip to Main Content

I am try to encrypt password in Node js by use of const bcrypt = require(‘bcrypt’); but its not hashing. When you try on terminal its hashing but not on database, just posting a plain text that was typed on an input.

Installation and imports

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' });
  }
});

I am expecting password to hashed, for example password: 123 , this $2b$10$8hL4zs.XUIXpc6sxf/kfYeKWAcwduZaMlUsZoRz8.cRc5kz9EzEV2 should be posted to database datatype password

3

Answers


  1. Chosen as BEST ANSWER

    Thanks everyone who contributed, I have solved the issue, I clear node js cache and it worked

    npm cache clean --force
    

  2. Hope this helps you.

    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();
    
    Login or Signup to reply.
  3. bcrypt.hashSync(plainPassword, 10);
    you can use instead of bcrypt.hash but be carefull because

    hash, asynchronously function
    hashSync, synchronous Function

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search