Skip to content

Integrate Auth0 with Tiger Data

Use Tiger Cloud or self-hosted TimescaleDB as the user store for an Auth0 custom database connection.

Auth0 is an identity platform that provides authentication and authorization as a service. With a custom database connection, Auth0 authenticates users against a database you own instead of storing them in Auth0.

This page shows you how to use Tiger Data as the user store for an Auth0 custom database connection, so that your users live in your service or database while Auth0 handles the login flow.

Warning

Auth0 runs custom database action scripts from its own infrastructure, so it must reach your service or database over the network. For Tiger Cloud, allowlist Auth0's IP addresses for your region. A self-hosted TimescaleDB instance must be reachable from Auth0, either directly or through a tunnel. Never expose a database to the internet without appropriate firewalling and encryption.

In this integration guide, you:

  • Create the users table in your service or database.
  • Configure an Auth0 custom database connection with action scripts.
  • Verify that Auth0 reads and writes users in your service or database.

Prerequisites for this integration guide

To follow these steps, you'll need:

Create the users schema in your service or database

Section titled “Create the users schema in your service or database”

Auth0's PostgreSQL action scripts hash passwords with bcrypt and read and write a users table. Create that table before you configure the connection.

  1. Connect to your service or database

    Connect to your service or database with your connection details.

  2. Create the users table

    Run the following SQL, adapted from Auth0's reference schema:

    CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    email VARCHAR(255) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    nickname VARCHAR(255),
    email_verified BOOLEAN
    );

    The password column stores a bcrypt hash, which the action scripts compute. Tiger Data never sees plaintext passwords.

Configure the custom database connection in Auth0

Section titled “Configure the custom database connection in Auth0”

Create a database connection in Auth0, store your connection details, and add the action scripts that read and write your users table.

  1. Create a database connection

    In the Auth0 Dashboard, go to Authentication > Database, then click Create DB Connection. Give the connection a name and create it.

  2. Enable your own database

    Open the connection, select the Custom Database tab, and turn on Use my own database. The Database Action Scripts editor appears.

  3. Store your connection string

    In the Settings section below the script editor, add a parameter for your connection details, for example DATABASE_CONNECTION_STRING, with a value like:

    postgres://<user>:<password>@<host>:<port>/<dbname>?sslmode=require

    The action scripts read this value from the configuration object, so your credentials stay out of the script body. Tiger Cloud requires an encrypted connection, so keep sslmode=require.

  4. Add the Login script

    Select the Login script, choose the PostgreSQL template, and adapt it to read your connection string from configuration:

    function login(email, password, callback) {
    const bcrypt = require('bcrypt');
    const postgres = require('pg');
    const conString = configuration.DATABASE_CONNECTION_STRING;
    postgres.connect(conString, function (err, client, done) {
    if (err) return callback(err);
    const query = 'SELECT id, nickname, email, password FROM users WHERE email = $1';
    client.query(query, [email], function (err, result) {
    done();
    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,
    });
    });
    });
    });
    }
  5. Add the Create and Get User scripts

    Add the Create script so Auth0 can register new users, hashing the password with bcrypt before insert:

    function create(user, callback) {
    const bcrypt = require('bcrypt');
    const postgres = require('pg');
    const conString = configuration.DATABASE_CONNECTION_STRING;
    postgres.connect(conString, function (err, client, done) {
    if (err) return callback(err);
    bcrypt.hash(user.password, 10, function (err, hashedPassword) {
    if (err) return callback(err);
    const query = 'INSERT INTO users(email, password) VALUES ($1, $2)';
    client.query(query, [user.email, hashedPassword], function (err) {
    done();
    return callback(err);
    });
    });
    });
    }

    Add the Get User script the same way, reading from configuration.DATABASE_CONNECTION_STRING.

  6. Add the Verify, Change Password, and Delete scripts

    Add the scripts for the operations you want Auth0 to perform in your service or database. Each one starts from the PostgreSQL template and reads the connection string from configuration. All three are optional: add Verify if you use email verification, Change Password if you allow password resets, and Delete if you want user deletions in Auth0 to remove the row.

    The Verify script marks an email address as verified:

    function verify(email, callback) {
    const postgres = require('pg');
    const conString = configuration.DATABASE_CONNECTION_STRING;
    postgres.connect(conString, function (err, client, done) {
    if (err) return callback(err);
    const query = 'UPDATE users SET email_verified = true WHERE email_verified = false AND email = $1';
    client.query(query, [email], function (err, result) {
    done();
    return callback(err, result && result.rowCount > 0);
    });
    });
    }

    The Change Password script hashes and stores a new password:

    function changePassword(email, newPassword, callback) {
    const bcrypt = require('bcrypt');
    const postgres = require('pg');
    const conString = configuration.DATABASE_CONNECTION_STRING;
    postgres.connect(conString, function (err, client, done) {
    if (err) return callback(err);
    bcrypt.hash(newPassword, 10, function (err, hash) {
    if (err) return callback(err);
    const query = 'UPDATE users SET password = $1 WHERE email = $2';
    client.query(query, [hash, email], function (err, result) {
    done();
    return callback(err, result && result.rowCount > 0);
    });
    });
    });
    }

    The Delete script removes a user by id:

    function remove(id, callback) {
    const postgres = require('pg');
    const conString = configuration.DATABASE_CONNECTION_STRING;
    postgres.connect(conString, function (err, client, done) {
    if (err) return callback(err);
    const query = 'DELETE FROM users WHERE id = $1';
    client.query(query, [id], function (err) {
    done();
    return callback(err);
    });
    });
    }
  7. Save the scripts

    Click Save. Auth0 connects to your service or database when a user authenticates against this connection.

To confirm Auth0 is working with your service or database:

  1. Create a user through Auth0

    In the Auth0 Dashboard, go to User Management > Users, click Create User, and select your custom database connection. Alternatively, sign up through an application that uses this connection.

  2. Query your service or database to confirm the user arrived

    Connect to your service or database and query the users table:

    SELECT id, email, nickname FROM users;

    You see a row for the user you just created, with a bcrypt hash in the password column, confirming that Auth0 is writing to Tiger Data.

You have successfully integrated Auth0 with Tiger Data.

  • Connection timeouts in the script editor: confirm Auth0 can reach your service or database. For Tiger Cloud, allowlist Auth0's IP addresses; self-hosted instances must be reachable from Auth0.
  • WrongUsernameOrPasswordError on valid credentials: make sure the Login and Create scripts use the same bcrypt hashing, and that existing rows store bcrypt hashes in the password column.

For other connectivity and authentication issues, see Troubleshoot Tiger Cloud integrations.