Post dbConnections/signup returns 400

I am trying to test my database create user script by hitting my dbConnections/signup endpoint with an insomnia Post Request. I have confirmed that my database has the “disable signups” option turned off.

Here is my request:
{
“client_id”: “myClientIdFromApplication”,
“email”: “john@test.com”,
“password”: “PASSWORD”,
“connection”: “myDBName”,
“username”: “johndoe”,
“given_name”: “John”,
“family_name”: “Doe”,
“name”: “John Doe”,
“nickname”: “johnny”,
“picture”: “http://example.org/jdoe.png
}

The response I get in Insomnia is a 400 (Bad Request) with this body:
{
“fromSandbox”: true,
“statusCode”: 400
}

If helpful here is my Create User Script (Derived from PostgreSQL template):

function create(user, 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 = configuration.DATABASE_URL;
  const client = new postgres.Client( {
    connectionString: conString,
    ssl: {sslmode: 'require', rejectUnauthorized: false}
  });
  client.connect();
  
  bcrypt.hash(user.password, 10, function (err, hashedPassword) {
    if (err) return callback(err);

    const query = 'INSERT INTO organizations(email, password) VALUES ($1, $2)';
    client.query(query, [user.email, hashedPassword], function (err, result) {
      // NOTE: always call `done()` here to close
      // the connection to the database
      client.close();

      return callback(err);
    });
  });
}

I am following this documentation so I am confused on to why I am running into this problem. Any help would be appreciated!

Hi @jfletcher,

Thanks for reaching out to the Auth0 Community!

I understand that you have encountered issues when creating a user, specifically with the 400 (Bad Request) error.

After looking at your request, everything looks good. I suspect that the error may be happening because the user already exists. And when I try to create an existing user, I could reproduce the same error.

Given that, could you please check if the user exists in your current database?

If so, could you please delete them and try creating them again?

Alternatively, you could try creating a user with a different email address to see if you can create users successfully.

Finally, there is also the option to use the Management API Create a user endoint to accomplish the same results.

Please let me know how this goes for you.

Thank you.

1 Like

Hi @reuben.tiow

Thanks for the response. The issue was related to another ticket that I have since resolved.

I am able to add a user to my database succesfully now; however, in Auth0’s response I am receiving a 400 error with message: “no pg_hba.conf entry for host "xxx", user "xxx", database "xxx", no encryption” Any idea how I am able to succesfully POST a user on my database but get an error in return?

1 Like

Hi @jfletcher,

Thank you for your response and clarification.

I now understand that you are using a Custom Database to create new users.

Having looked closely at your error message, it appears that it is returned from your Custom Database scripts and not an error from Auth0. It is likely an issue involving your Get User, Create User or Login scripts.

Given that you were able to add a new user to your custom database successfully, I suspect the issue may be happening in your Login script.

In this situation, could you please make sure that your Login script follows this PostgreSQL template?

Lastly, you may find the Real-time Webtask Logs Extension useful in debugging your scripts.

Please let me know how this works for you.

Thank you.

Hi @rueben.tiow thanks for the response.
The culprit was my Login script. Correct me if I am wrong, but that means login gets called after the create user script? If the user that was just created gets logged in, what is the best way to handle that auth back to my server/front end? Is there any good resources you would recommend?

Also, I had to change the postgresSQL template for it to work as the pg version had to be 8.7.1

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@8.7.1');

  const conString = configuration.DATABASE_URL;
  const client = new postgres.Client( {
    connectionString: conString,
    ssl: {sslmode: 'require', rejectUnauthorized: false}
  });
  client.connect();

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

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

    const user = result.rows[0];

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

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

1 Like

Hi @jfletcher,

Thank you for your reply.

Yes, that is correct. This doc elaborates further that a newly created user in Auth0 triggers the following scripts in order:

  1. Get User: Verifies that the user does not already exist in Auth0 or the external DB
  2. Create: Creates the user in the external DB
  3. Login: Verifies that the new user was created successfully.

Have you made sure to enable the custom database connection for your application? If the Get User, Create User, and Login scripts are configured correctly, your users should be able to log into your app using the Universal Login. If not, I recommend reading our Custom Database Action scripts - Login script documentation

Please let me know if you have any further questions.

Thanks,
Rueben