This tutorial demonstrates how to add user login to a Django application using Auth0. We recommend you to Log in to follow this quickstart with examples configured for your account.
I want to integrate with my app15 minutes
I want to explore a sample app2 minutes
Get a sample configured with your account settings or check it out on Github.
Get Your Application Keys
When you signed up for Auth0, a new application was created for you, or you could have created a new one.
You will need some details about that application to communicate with Auth0. You can get these details from the Application Settings section in the Auth0 dashboard.
You need the following information:
- Client ID
Configure Callback URLs
A callback URL is a URL in your application where Auth0 redirects the user after they have authenticated.
You need to whitelist the callback URL for your app in the Allowed Callback URLs field in your Application Settings. If you do not set any callback URL, your users will see a mismatch error when they log in.
Configure Logout URLs
A logout URL is a URL in your application that Auth0 can return to after the user has been logged out of the authorization server. This is specified in the
returnTo query parameter.
You can set the logout URL for your app in the Allowed Logout URLs field in your Application Settings. If you don't set a logout URL, your users will be unable to log out of the application and will get an error.
Create a Django Application configured to use Auth0
This guide will use
social_django which is the Django implementation of Python Social Auth. It adds an OAuth stack to the user authentication & authorization system bundled by the Django Web Framework.
Install the Dependencies
Add the following dependencies to your
Once the dependencies are listed in requirements.txt, run the following command:
Create a Django Project
This guide assumes you already have a Django application set up. If that is not the case, follow the steps in the Django Tutorial.
The sample project was created with the following commands:
settings.py file contains the configuration of your Django project.
Add one entry for
social_django and for your application into the
- Set the Callback URL in the Application Settings to: Add your Auth0 domain, the Client Id and the Client Secret. You can get this information the application settings in the Auth0 Dashboard.
SOCIAL_AUTH_AUTH0_SCOPE variable with the scopes the application will request when authenticating. Check the Scopes documentation for more information.
Initialize the Database
social_django application defined in
INSTALLED_APPS requires a database. Run the following command to create all the required databases for the applications defined in
Create the Auth0 Authentication Backend
social_django application is now configured. The next step is to create an authentication backend that bridges
social_django with Auth0.
Create a file to implement the custom
Auth0 authentication backend.
Register the authentication backends in
settings.py. Add the custom backend for
ModelBackend for users to be able to login with username/password method.
Configure the login, redirect login and redirect logout URLs as set below. The LOGIN_URL ends with
auth0 as it needs to match the
name property of the custom backend defined above.
Add a handler for the
index view in your
views.py to render the
Add a link to
/login/auth0 in the
Display User Information
After the user is logged in, you can access the user information from the
request.user property. Add a handler for the
/dashboard endpoint in the
Add the following snippet to
dashboard.html to display the user information.
To log a user out, add a link to
Then, add a
logout method in
views.py to clear the session and redirect the user to the Auth0 logout endpoint.
Add URL Mappings
In previous steps we added methods to the
views.py file. We need to map those methods to URLs.
Django has a URL dispatcher that lets you map URL patterns to views.
Add mappings for the root folder, the dashboard folder, and the authentication applications in
Run the Sample
To run the sample from a terminal, change the directory to the root folder of the project and execute the following line:
The application will be accessible on http://localhost:3000. Follow the Log In link to log in or sign up to your Auth0 tenant. Upon successful login or signup, you should be redirected to the user's profile page.