To use a custom database with Auth0, you'll need to set up a Custom Database Connection. Here's what's required:
Database Requirements
Your database needs to be accessible from Auth0's servers. This means:
- The database must be reachable via the internet (or through a secure tunnel)
- Appropriate firewall rules to allow Auth0's IP addresses
- SSL/TLS encryption for secure connections
- Proper authentication credentials (username/password, connection string, etc.)
Required Scripts
You'll need to implement several Node.js scripts in the Auth0 dashboard that act as the interface between Auth0 and your database:
Login Script - Authenticates users by verifying credentials against your database. This script receives the username/password and should return the user profile if authentication succeeds.
Get User Script - Retrieves a user profile by email or user ID. This is used for operations like password resets or when Auth0 needs to fetch user information.
Create Script (optional) - Adds new users to your database when they sign up through Auth0.
Verify Script (optional) - Marks a user's email as verified in your database.
Change Password Script (optional) - Updates a user's password in your database.
Delete Script (optional) - Removes a user from your database.
Database Schema Considerations
Your user table/collection should typically include:
- Unique identifier (user_id)
- Email address
- Password (properly hashed - bcrypt is recommended)
- Email verification status
- Any additional profile fields you need
Configuration Steps
In the Auth0 dashboard, you'll need to:
- Create a new Database Connection
- Enable "Use my own database"
- Configure the database action scripts
- Set up any required configuration variables (connection strings, API keys, etc.)
- Test the connection with sample credentials
- Enable the connection for your applications
Security Best Practices
When implementing custom database connections:
- Never store passwords in plain text - use bcrypt or similar hashing
- Implement rate limiting to prevent brute force attacks
- Use parameterized queries to prevent SQL injection
- Store sensitive configuration (like connection strings) in Auth0's configuration variables, not hardcoded in scripts
- Implement proper error handling that doesn't expose sensitive information
- Consider implementing account lockout mechanisms for failed login attempts
Migration Considerations
Auth0 offers two migration approaches:
- Bulk Migration: Import all users at once before going live
- Automatic Migration: Migrate users gradually as they log in, which Auth0 facilitates through the custom database scripts
The automatic migration feature is particularly useful for large user bases, as it allows you to migrate users seamlessly without requiring password resets.