Custom database login

Hi I have set a custom database with an action script on the login event this is the code:

function login(email, password, callback) {
  //this example uses the "pg" library
  //more info here: https://github.com/brianc/node-postgres

  const bcrypt = require('bcrypt');
  const postgres = require('pg');

  const conString = 'user://password:@host/database';
  var pool = new postgres.Pool({connectionString: conString});
  pool.connect(function (err, client, done) {
    if (err) return callback(err);

    const query = 'SELECT id, name, email, encrypted_password, handle FROM users WHERE email = $1';
    client.query(query, [email], function (err, result) {
      // NOTE: always call `done()` here to close
      // the connection to the database
      done();
      

      if (err || result.rows.length === 0) return callback(err || new WrongUsernameOrPasswordError(email));

      const user = result.rows[0];

      bcrypt.compare(password, user.encrypted_password, function (err, isValid) {
        if (err || !isValid) return callback(err || new WrongUsernameOrPasswordError(email));

        return callback(null, {
          user_id: user.id,
          name: user.name,
          email: user.email,
          handle: user.handle
        });
      });
    });
  });
}

When I use the try connection feature or the test on the custom database with a user and password stored on the db given on the connection string, it works oka and return the token with the user data, but If i try to loging to my spa app that I setted up connection with the custom db as a source, I can’t logging with the same user and password.

Shuld the user/password beeing able to login to my spa application ? Or maybe I missing something.

Hi @hacosta

Welcome to the Auth0 Community!

Thank you for posting your question and I appreciate your patience. I’ve checked your tenant configuration and your SPA application is not enabled for custom database connection. To do that go to Authentication → Database → Application and change from grey to green switch button.

I hope it will help you!

Thanks
Dawid

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