# Custom database login generate 401 error

**URL:** <https://community.auth0.com/t/custom-database-login-generate-401-error/125012>\
**Category:** Get Help\
**Tags:** identifier-first, login-experience\
**Created:** [January 17, 2024, 3:17pm UTC](https://community.auth0.com/t/custom-database-login-generate-401-error/125012 "2024-01-17T15:17:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![malanciault](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/malanciault/32/51306_2.png) [@malanciault](https://community.auth0.com/u/malanciault)\
**Post date:** [January 17, 2024, 3:17pm UTC](https://community.auth0.com/t/custom-database-login-generate-401-error/125012/1 "2024-01-17T15:17:01Z")

</div>

I created a custom database using our own MySQL database.

The connection to MySQL works fine.

The user is found in the table.

However, bcrypt.compare aways returns false even though I am sure that the password entered is the correct one.

Here is the script I use:

```auto
function login(email, password, context, callback) {
  const mysql = require('mysql');
  const bcrypt = require('bcrypt');

  const connection = mysql.createConnection({
    host: configuration.MYSQL_HOST,
    user: configuration.MYSQL_USER,
    password: configuration.MYSQL_PASSWORD,
    database: configuration.MYSQL_DATABASE
  });

  connection.connect();
	console.log("connected to database");
  
  const query = 'SELECT user_id, firstname, lastname, email, password FROM user WHERE email = ? AND app_org_id = ' + context.organization.metadata.app_org_id;

  connection.query(query, [email], function(err, results) {

    if (err) return callback(err);
    if (results.length === 0) return callback(new WrongUsernameOrPasswordError(email));
    const user = results[0];
    
    console.log("user found, user_id is " + user.user_id);
    console.log("comparing password with hash");
    bcrypt.compare(password, user.password, function(err, isValid) {
      if (err || !isValid) {
        console.log("bcrypt.compare failed");
        return callback(err || new WrongUsernameOrPasswordError(email));
      }

      callback(null, {
        user_id: user.user_id.toString(),
        nickname: user.email,
        email: user.email
      });
    });
  });
}

```

Here is are the logs in Real-time Webtask Logs:

```auto
10:09:03 AM:
 connected to database
10:09:04 AM:
 user found, user_id is 616283
10:09:04 AM:
 comparing password with hash
10:09:04 AM:
 bcrypt.compare failed
10:09:04 AM:
 finished webtask request

```

What could be the problem ?

---

<div class="post-metadata">

**Author:** ![pavan.bn](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/pavan.bn/32/61674_2.png) [@pavan.bn](https://community.auth0.com/u/pavan.bn)\
**Post date:** [January 17, 2024, 3:54pm UTC](https://community.auth0.com/t/custom-database-login-generate-401-error/125012/3 "2024-01-17T15:54:56Z")

</div>

Hi @malanciault

Thanks for reaching out to Auth0 Community!

On high level i can suggest is that your external database password must be encrypted and that will be compared with the user entered password plain text, if this not happens then bcrypt.compare failed will come.

Also please check this github link with good discussion on this issue who has faced earlier [bcrypt.compare always returns false · Issue #906 · kelektiv/node.bcrypt.js · GitHub](https://github.com/kelektiv/node.bcrypt.js/issues/906#:~:text=SuhailHamid42-,commented,-on%20May%2027)

previous community post with similar issue

[https://community.auth0.com/t/joomla-login-bcrypt-compare-always-false/5163](https://community.auth0.com/t/joomla-login-bcrypt-compare-always-false/5163)

Hope this helps!

Regards,  
Pavan.

---

<div class="post-metadata">

**Author:** ![malanciault](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/malanciault/32/51306_2.png) [@malanciault](https://community.auth0.com/u/malanciault)\
**Post date:** [January 17, 2024, 6:57pm UTC](https://community.auth0.com/t/custom-database-login-generate-401-error/125012/4 "2024-01-17T18:57:47Z")

</div>

Thanks Pavan!

> On high level i can suggest is that your external database password must be encrypted and that will be compared with the user entered password plain text, if this not happens then bcrypt.compare failed will come.

I confirm that the password stored in the database is indeed encrypted using PHP [password\_hash](https://www.php.net/password_hash) and BCRYPT algorithm.

With the links you mentionned, I came accross this [bcrypt.compare return false always. · Issue #685 · kelektiv/node.bcrypt.js · GitHub](https://github.com/kelektiv/node.bcrypt.js/issues/685#issuecomment-1056002440) which solved the problem.

---

<div class="post-metadata">

**Author:** ![malanciault](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/malanciault/32/51306_2.png) [@malanciault](https://community.auth0.com/u/malanciault)\
**Post date:** [January 17, 2024, 7:10pm UTC](https://community.auth0.com/t/custom-database-login-generate-401-error/125012/5 "2024-01-17T19:10:11Z")

</div>

If it can help other people, here is the Database Action Script that worked (using MysQL database)

```auto
function login(email, password, context, callback) {
  const mysql = require('mysql');
  const bcrypt = require('bcrypt');

  const connection = mysql.createConnection({
    host: configuration.MYSQL_HOST,
    user: configuration.MYSQL_USER,
    password: configuration.MYSQL_PASSWORD,
    database: configuration.MYSQL_DATABASE
  });

  connection.connect();
  
  const query = 'SELECT user_id, firstname, lastname, email, password FROM user WHERE email = ?';

  connection.query(query, [email], function(err, results) {

    if (err) return callback(err);
    if (results.length === 0) return callback(new WrongUsernameOrPasswordError(email));
    const user = results[0];

    const hashPass = /^\$2y\$/.test(user.password) ? '$2a$' + user.password.slice(4) : user.password;
    bcrypt.compare(password, hashPass, function(err, isValid) {
      if (err || !isValid) {
        console.log("bcrypt.compare failed");
        return callback(err || new WrongUsernameOrPasswordError(email));
      }

      callback(null, {
        user_id: user.user_id.toString(),
        nickname: user.email,
        email: user.email
      });
    });
  });
}

```

---

<div class="post-metadata">

**Author:** ![pavan.bn](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/pavan.bn/32/61674_2.png) [@pavan.bn](https://community.auth0.com/u/pavan.bn)\
**Post date:** [January 18, 2024, 4:27am UTC](https://community.auth0.com/t/custom-database-login-generate-401-error/125012/6 "2024-01-18T04:27:58Z")

</div>

Hi @malanciault,

Great to here the issue got resolved, and Thank you for sharing the updated code which will definitely help others.

Regards,  
Pavan.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [February 1, 2024, 4:28am UTC](https://community.auth0.com/t/custom-database-login-generate-401-error/125012/7 "2024-02-01T04:28:32Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
