M M
M M

Reputation: 1

How to fix : " Error: data and hash arguments required "

I'm trying login with a user that I have registered in my form but I have this error:

Error: data and hash arguments required

The user is registered in my database with a Username, Fullname and the hashed password thanks to bcrypt.

const pool = require("./pool");
const bcrypt = require("bcrypt");

function User() {}

User.prototype = {
  find: function(user = null, callback) {

  if (user) {
      var field = Number.isInteger(user) ? "id" : "username";
    }
    let sql = `SELECT * FROM users WHERE ${"" + field + ""} = ?`;
    pool.query(sql, user, function(err, result) {
      if (err) console.log(err);
      callback(result);
    });
  },

  create: function(body, callback) {
    let pwd = body.password;
    body.password = bcrypt.hashSync(pwd, 10);

    var bind = [body.username, body.fullname, body.password];
    console.log(bind);

    let sql =
      "INSERT INTO users(username, fullname, password) VALUES (?, ?, ?)";

    pool.query(sql, bind, function(err, lastId) {
      if (err) throw err;
      callback(lastId);
    });
  },

  login: function(username, password, callback) {
    this.find(username, function(user) {
      if (user) {
        if (bcrypt.compareSync(password, user.password)) {
          callback(user);
          return;
        }
      }
    });
  }
};

module.exports = User;

Once i press my login button in my form this is the error I'm getting

  throw err; // Rethrow non-MySQL errors
  ^
    at Object.compareSync (/Users/me/happy/node_modules/bcrypt/bcrypt.js:167:15)
    at /Users/me/happy/core/user.js:46:20
    at Query.<anonymous> (/Users/me/happy/core/user.js:16:7)
    at Query.<anonymous> (/Users/me/happy/node_modules/mysql/lib/Connection.js:525:10)
    at Query._callback (/Users/me/happy/node_modules/mysql/lib/Connection.js:491:16)
    at Query.Sequence.end (/Users/me/happy/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
    at Query._handleFinalResultPacket (/Users/me/happy/node_modules/mysql/lib/protocol/sequences/Query.js:139:8)
    at Query.EofPacket (/Users/me/happy/node_modules/mysql/lib/protocol/sequences/Query.js:123:8)
    at Protocol._parsePacket (/Users/me/happy/node_modules/mysql/lib/protocol/Protocol.js:291:23)
    at Parser._parsePacket (/Users/me/happy/node_modules/mysql/lib/protocol/Parser.js:433:10)

Upvotes: 0

Views: 8034

Answers (3)

Mohammed Sarfaraz
Mohammed Sarfaraz

Reputation: 460

Make sure you are passing valid values in both arguments. userpassword and dbpassword should not be undefined or null

Upvotes: 2

Rajneshwar Singh
Rajneshwar Singh

Reputation: 122

Please check password and user.password is not equal to undefined. Because the same function is working fine for me. If you are still facing the same issue then please try bcrypt.compare. bcrypt.compare takes 3 parameters passwordToCheck, passwordHash and a callback.

login: function(username, password, callback) {
    this.find(username, function(user) {
        if (user) {
            bcrypt.compare(password,  user.password, function(err, match) {
                if (err) throw new Error(err);
                else if (match == false) {
                    return res.json({
                        success: false,
                        message: 'Wrong Password'
                    })
                } else {
                    callback(user);
                    return;
                }
            });
        }
    });
};

Upvotes: 0

Juan Velasquez
Juan Velasquez

Reputation: 383

Try to generate a salt synchronously, updating this line

body.password = bcrypt.hashSync(pwd, 10);

for

body.password = bcrypt.hashSync(pwd, bcrypt.genSaltSync(10));

Upvotes: 0

Related Questions