You can use Auth0 to create and maintain your own user store while also providing a secure authentication system on top of it. Auth0 lets you store users in your own database. Furthermore, Auth0 uses OAuth 2.0 as an authorization framework with which you can be confident in terms of security. Using a custom database as a user store in Auth0 facilitates user migration and adds an OAuth and OpenID Connect layer to your existing user database. You can configure any database or web service, including ASP.NET Membership Provider, MongoDB, MySQL, PostgreSQL, as an Auth0 custom database. In this article, you will learn how to set up PostgreSQL as a custom database for Auth0.
Limited Access: Some features are available depending on your Auth0 subscription. For more information, see Auth0 pricing plans.
Don't already have an Auth0 account yet? Sign up for free right now!
Once you log in, navigate to the Database connections page (Auth0 Dashboard > Authentication > Database). Click on the Create DB Connection button, give your database a nice name, and click the Create button. Go to the Custom Database tab and enable Use my own database to configure the custom database.
- Create: The Create script inserts a new user with
passwordinto your PostgreSQL database.
- Login: The Login script verifies the identity of the user in your PostgreSQL database whenever a user needs to authenticate.
- Verify: The Verify script updates the verification status of a user's email address in your database.
- Change Password: The Change Password script modifies the password for the user's email address. It changes the user's password in the database for future logins (forgot password functionality).
- Get User: The Get User script retrieves a specified user's information from your database. It returns
undefinedif the user is not present in your database.
- Delete: The Delete script removes the specified user identity from your database.
Fortunately, Auth0 provides templates for these custom database scripts. You can use the template and update the connection string with your database URL. Refer to the official documentation for additional information on Auth0 custom database action scripts. Let's start by creating a PostgreSQL database to connect with Auth0.
You cannot use the database in your local environment. You should make your database accessible via the internet or use tools like Database as A Service. In this tutorial, I'll use ElephantSQL, which offers a fully configured and cloud-hosted PostgreSQL database in few minutes. However, it does not display any data inside your database; instead, you can run SQL commands to show your data. You can design your own database and host it on any cloud provider like AWS, Google Cloud or Heroku.
Head over to https://www.elephantsql.com/ and create an account for yourself or log in if you already have one. It takes you to the dashboard page; Click on the Create New Instance to create a new database instance. Give a suitable name to your database instance and choose a plan and a region. I'll use the free plan for demonstration purposes. Configure the database to meet your requirements, then click Create instance.
If you encounter any error in creating an instance, refer to the ElephantSQL documentation for troubleshooting.
Now, navigate to the freshly created database instance and copy the URL from the details page. You'll need this to connect your Auth0 application to this database. Anyone can access your database with this connection string. It would help if you were to keep it very secret for security purposes.
PS: Auth0 provides several IP addresses to configure your database network connections if you're using a firewall. You can find them under the action scripts.
Note: Using your own database leads to several security concerns. Allowing your database accessible over the internet without any firewalls or security measures may result in data leakage.
Switch to the Browser tab and execute the following command.
CREATE TABLE users( id UUID DEFAULT uuid_generate_v4(), nickname VARCHAR(255), email VARCHAR(255) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL, email_Verified BOOLEAN DEFAULT FALSE );
The above command creates a new table called
users to store the user information from Auth0. You need at least the above five columns to save and validate the user identity.