0

I'm trying to log a user with nodeJs and MySql

Here my function to connect a User:

export async function connectUser(email, password, cb) {

  console.log('Entry connectuser');
  let db = dbConnect();

  await db.query('SELECT id, email, password FROM user WHERE email = ?', [email], async (error, result) => {
    if (error) return cb(error);

    const user = result[0];
    
    console.log(result);
    console.log(user.email);
    console.log(user.password);
    console.log(user.id);
    
    const isValid = await bcrypt.compare(password, user.password);
    if (isValid === true) {
      const token = jwt.sign({id: user.id, email: user.email }, secret, {expiresIn: 86400 });
      return cb(null, token);
    }
    
    return cb(new Error('Invalid Credentials'));
    
});

I get this error: "UnhandledPromiseRejectionWarning: Error: Illegal arguments: object, string" on the bcrypt.compare()

I suppose that it's an error of async/await but i can't fix it... some ideas?

1 Answers1

0

As described in the doc, you should use bcrypt.compare like that:

    bcrypt.compare(req.body.password,user.password, function(err, res) {
  if (err){
    // handle error
  }
  if (res)
    // Send JWT
  } else {
    // response is OutgoingMessage object that server response http request
    return response.json({success: false, message: 'passwords do not match'});
  }
});
majkl zumberi
  • 156
  • 1
  • 8