Custom MySql Connection Is not working Showing 401 - Unauthorized Error

Hi All,

I am trying to connect own mysql server using CUSTOM MySql Database.
I have added following script in login Database Action Scripts.

 function login(email, password, callback) {
  var connectionConfiguration = {
    host: '35.154.162.95',
    user: 'test',
    password: 'LUws4tVNP0s9m2mJ',
    database: 'test'
  };
  var connection = mysql(connectionConfiguration);
  connection.connect(function(err) {
    if (err) {
      console.log(connectionConfiguration);
      console.error('error connecting: ' + err.stack);
      return;
    }
    console.log('connected as id ' + connection.threadId);
});
  var query = "SELECT * FROM users WHERE email = ?";

  connection.query(query, [email], function (err, results) {
    if (err) return callback(err);
    if (results.length === 0) return callback(new WrongUsernameOrPasswordError(email));
    var user = results[0];

    bcrypt.compare(password, user.password, function (err, isValid) {
      if (err) {
        callback(err);
      } else if (!isValid) {
        callback(new WrongUsernameOrPasswordError(email));
      } else {
        callback(null, {
          id: user.id.toString(),
          nickname: user.nickname,
          email: user.email
        });
      }
    });

  });
}

But it is showing the error of 401 - Unauthorized .

Please help me out.

After debugging the issue, i found this.

But still getting 401 - Unauthorized .

@umakant Did you solve this? even I am getting same error. @support4 can you please help us to solve this problem?

I’m experience a similar issue but with the Login script for Azure DB. Script template - the Create and Verify scripts works just fine for me, but the rowCollectionOnRequestCompletion added in the connection on Login makes me get a 401 Unauthorized error whenever open the request.

I have the same issue - any fixes so far?

I am getting the same error. Any idea how to fix this?

I had something similar happening, but it was due to async/wait issue. I mean my code was not all IF-ELSE so it would not wait for the async fn and come down and do a blank return at end which is always unauthorized.

Try adding return callback(new WrongUsernameOrPasswordError('hello')); or any default return at the end and see if that is what is being returned instead of bcrypt.compare()