Docs

Django

View on Github

Django

Gravatar for andres.aguiar@auth0.com
By Andres Aguiar
Auth0

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 explore a sample app

2 minutes

Get a sample configured with your account settings or check it out on Github.

View on Github
System requirements: python 3.5 and up | django 2.1 and up | social-auth-app-django 3.1 and up | python-jose 3.0 and up | python-dotenv 0.9 and up

New to Auth? Learn How Auth0 works, how it integrates with Regular Web Applications and which protocol it uses.

Configure Auth0

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
  • Domain

If you download the sample from the top of this page these details are filled out for you.

If you have more than one application in your account, the sample comes with the values for your Default App.

App Dashboard

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.

If you are following along with the sample project you downloaded from the top of this page, you should set the Callback URL to http://localhost:3000/complete/auth0.

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.

This quickstart uses Django 2, if you are using Django 1 instead, follow the sample.

Install the Dependencies

Add the following dependencies to your requirements.txt:

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:

Django Settings

The settings.py file contains the configuration of your Django project.

Add one entry for social_django and for your application into the INSTALLED_APPS entry.

  1. 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.

Set the SOCIAL_AUTH_AUTH0_SCOPE variable with the scopes the application will request when authenticating. Check the Scopes documentation for more information.

Initialize the Database

The 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 INSTALLED_APPS:

Create the Auth0 Authentication Backend

The 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.

The callback URL will be calculated by social-auth by concatenating /callback with the backend name property, so it will be /callback/auth0.

Register the authentication backends in settings.py. Add the custom backend for Auth0 and 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.

Trigger Authentication

Add a handler for the index view in your views.py to render the index.html

Add a link to /login/auth0 in the index.html template.

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 views.py file.

Add the following snippet to dashboard.html to display the user information.

Logout

To log a user out, add a link to /logout in dashboard.html.

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 urls.py.

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.

login page

Use Auth0 for FREE