Authenticating Users on a database with username and password

Often times, applications rely on user databases for authentication. ASP.NET Membership is an example of a commonly used one.

Auth0 allows you to easily connect with these repositories, reuse them as identity providers, preserving their existing credentials, but also adding all the other awesome features Auth0 has.

In this tutorial we will guide you through a series of steps to plug your users database to Auth0.

Create the database connection

Log in into Auth0, select the Connections menu option, and then Database. Create a new name for the database connection. You can choose any name. It is not important at this time.

Customize the database connection

Auth0 ships with multiple templates to connect with common and widely used database systems: MySQL, SQL Server, SQL Azure, MongoDb, Couchbase, Postgres, among others.

In this tutorial, we will use MySQL. Select MySQL from one of the templates available:

The following code will be generated for you in the editor below:

function login (email, password, callback) {
  var connection = mysql({
    host     : 'localhost',
    user     : 'me',
    password : 'secret',
    database : 'mydb'


  var query = "SELECT id, nickname, email, password " +
             "FROM users WHERE email = ?";

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

    if (!bcrypt.compareSync(password, user.password)) {
      return callback();

    callback(null,   {
      nickname:    user.nickname,



As you can see, this script connects to a MySQL database and executes a query to retrieve the first user with email == It then validates that the passwords match (with the bcrypt.compareSync method), and if successful, it will finally return an object with some user profile information: id, nickname, email.

This script assumes you have a users table with all these columns. You can of course tweak this script in order to adjust it to your own requirements.


At the bottom of the editor you will find a way of storing parameters securely. This is convenient for storing the credentials used to connect to the database:

In the script you would refer to these parameters as: configuration.PARAMETER_NAME. For example, you could write:

function login (username, password, callback) {
  var connection = mysql({
    host     : 'localhost',
    user     : 'me',
    password : configuration.MYSQL_PASSWORD,
    database : 'mydb'

Debugging and troubleshooting

You can test the script using the try button. If the result is okay you will see the resulting profile:

Auth0 Login Widget

After you have enabled the database connection, Auth0's widget will automatically change the appearance to allow users to enter username and password. These will be inputs to your script.

How it works

The script runs in a JavaScript sandbox where you can use the full power of the language and selected libraries. The current API supports:




To return an error simple call the callback with an error as the first parameter:


There are three different types of errors you can return from a DB Connection:

  • new WrongUsernameOrPasswordError(<email or user_id>, <message>): Use this error when you know who is the user and you want to keep track of the wrong password.
  • new ValidationError(<error code>, <message>): Generic error with an error code.
  • new Error(<message>): Simple errors (no error code).


callback(new ValidationError('email-too-long', 'Email is too long.'));

Do you need support for other libraries? Contact us: