Add Login to your Django Application

Auth0 allows you to add authentication and gain access to user profile information in your application. This guide demonstrates how to integrate Auth0 with a Python Django application using the Authlib SDK.

1

Configure Auth0

To use Auth0 services, you’ll need to have an application set up in the Auth0 Dashboard. The Auth0 application is where you will configure how you want authentication to work for the project you are developing.

Configure an application

Use the interactive selector to create a new Auth0 application or select an existing application that represents the project you want to integrate with. Every application in Auth0 is assigned an alphanumeric, unique client ID that your application code will use to call Auth0 APIs through the SDK.

Any settings you configure using this quickstart will automatically update for your Application in the Dashboard, which is where you can manage your Applications in the future.

If you would rather explore a complete configuration, you can view a sample application instead.

Configure Callback URLs

A callback URL is a URL in your application that you would like Auth0 to redirect users to after they have authenticated. If not set, users will not be returned to your application after they log in.

Configure Logout URLs

A logout URL is a URL in your application that you would like Auth0 to redirect users to after they have logged out. If not set, users will not be able to log out from your application and will receive an error.

2

Install dependencies

For this integration, you will add several library dependencies, such as Authlib. Create a requirements.txt file in your project directory, and include the following:

authlib ~= 1.0
django ~= 4.0
python-dotenv ~= 0.19
requests ~= 2.27

Was this helpful?

/

Run the following command from your shell to make these dependencies available:

pip install -r requirements.txt

3

Configure your .env file

Next, create an .env file in your project directory. This file will hold your client keys and other configuration details.

AUTH0_CLIENT_ID={yourClientId}
AUTH0_CLIENT_SECRET={yourClientSecret}
AUTH0_DOMAIN={yourDomain}

Was this helpful?

/

4

Create an application

If you already have a Django application setup, skip to the next step. For a new application project, run the following command:

django-admin startproject webappexample

Change to the new project folder:

cd webappexample

5

Update settings.py

Open the webappexample/settings.py file to review the .env values.

At the top of the file, add the os and dotenv imports.

Next, beneath the BASE_DIR definition, add the TEMPLATE_DIR variable.

Next, find the TEMPLATES variable and update the DIRS value to add our TEMPLATE_DIR string. This determines the path of the template files, which you will create in a future step. Keep any other content of this array the same.

At the end of this file, add the code to load the Auth0 config.

6

Setup your application

To begin creating your application, open the webappexample/views.py file in your IDE.

Import all the libraries your application needs.

Now you can configure Authlib to handle your application's authentication with Auth0.

Learn more about the configuration options available for Authlib's OAuth register() method from their documentation.

7

Setup your route handlers

In this example, you will add four routes for your application: the login, callback, logout, and index routes.

  • login - When visitors to your app visit the /login route, they will reach Auth0 to begin the authentication flow.

  • callback - After your users finish logging in with Auth0, they will return to your application at the /callback route. This route saves the session for the user and bypasses the need for them to login again when they return.

  • logout - The /logout route signs users out from your application. This route clears the user session in your app and redirects to the Auth0 logout endpoint to ensure the session is no longer saved. Then, the application redirects the user to your home route.

  • index - The home route will render an authenticated user's details or allow visitors to sign in.

8

Register your routes

Replace the contents of your webappexample/urls.py file with the code on the right to connect to these new routes.

This will route the /login, /callback, /logout and / routes to the correct handlers.

9

Add templates

Next, you will create a template file used in the home page route.

Create a new sub-directory within the webappexample folder named templates, and create a index.html file.

The index.html file will contain template code to display the user's info if logged in or present them with a login button if logged out.

10

Run your application

You're ready to run your application! From your project directory, open a shell and use:

python3 manage.py migrate
python3 manage.py runserver 3000

Was this helpful?

/

Your application should now be ready to open from your browser at http://localhost:3000.

checkpoint.header

Visit http://localhost:3000 to verify. You should find a login button routing to Auth0 for login, then back to your application to see your profile information.

Ṅḛḛẋţ Ṣţḛḛṗṡ

ḚḚẋͼḛḛḽḽḛḛṇţ ẁṓṓṛḳ! ḬḬϝ ẏẏṓṓṵṵ ṃααḍḛḛ ḭḭţ ţḥḭḭṡ ϝααṛ, ẏẏṓṓṵṵ ṡḥṓṓṵṵḽḍ ṇṓṓẁ ḥααṽḛḛ ḽṓṓḡḭḭṇ, ḽṓṓḡṓṓṵṵţ, ααṇḍ ṵṵṡḛḛṛ ṗṛṓṓϝḭḭḽḛḛ ḭḭṇϝṓṓṛṃααţḭḭṓṓṇ ṛṵṵṇṇḭḭṇḡ ḭḭṇ ẏẏṓṓṵṵṛ ααṗṗḽḭḭͼααţḭḭṓṓṇ.

Ṫḥḭḭṡ ͼṓṓṇͼḽṵṵḍḛḛṡ ṓṓṵṵṛ ʠṵṵḭḭͼḳṡţααṛţ ţṵṵţṓṓṛḭḭααḽ, ḅṵṵţ ţḥḛḛṛḛḛ ḭḭṡ ṡṓṓ ṃṵṵͼḥ ṃṓṓṛḛḛ ţṓṓ ḛḛẋṗḽṓṓṛḛḛ. Ṫṓṓ ḽḛḛααṛṇ ṃṓṓṛḛḛ ααḅṓṓṵṵţ ẁḥααţ ẏẏṓṓṵṵ ͼααṇ ḍṓṓ ẁḭḭţḥ ÀÀṵṵţḥ0, ͼḥḛḛͼḳ ṓṓṵṵţ:

  • ÀÀṵṵţḥ0 Ḍααṡḥḅṓṓααṛḍ - Ḻḛḛααṛṇ ḥṓṓẁ ţṓṓ ͼṓṓṇϝḭḭḡṵṵṛḛḛ ααṇḍ ṃααṇααḡḛḛ ẏẏṓṓṵṵṛ ÀÀṵṵţḥ0 ţḛḛṇααṇţ ααṇḍ ααṗṗḽḭḭͼααţḭḭṓṓṇṡ
  • ÀÀṵṵţḥ0 Ṁααṛḳḛḛţṗḽααͼḛḛ - Ḍḭḭṡͼṓṓṽḛḛṛ ḭḭṇţḛḛḡṛααţḭḭṓṓṇṡ ẏẏṓṓṵṵ ͼααṇ ḛḛṇααḅḽḛḛ ţṓṓ ḛḛẋţḛḛṇḍ ÀÀṵṵţḥ0’ṡ ϝṵṵṇͼţḭḭṓṓṇααḽḭḭţẏẏ

Ḍḭḭḍ ḭḭţ ẁṓṓṛḳ?

ÀÀṇẏẏ ṡṵṵḡḡḛḛṡţḭḭṓṓṇ ṓṓṛ ţẏẏṗṓṓ?

ḚḚḍḭḭţ ṓṓṇ ḠḭḭţḤṵṵḅ
ṗααḡḛḛṡ.ţṵṵţṓṓṛḭḭααḽṔααḡḛḛ.ṇḛḛẁ.ααͼţḭḭṓṓṇḤḛḛṛṓṓ.ṡḭḭḡṇṲṲṗḤḛḛṛṓṓ.ḅṵṵţţṓṓṇṪḛḛẋţ

Ṣḭḭḡṇ ṵṵṗ ϝṓṓṛ ααṇ ṓṓṛ ḽṓṓḡ ḭḭṇ ţṓṓ ẏẏṓṓṵṵṛ ḛḛẋḭḭṡţḭḭṇḡ ααͼͼṓṓṵṵṇţ ţṓṓ ḭḭṇţḛḛḡṛααţḛḛ ḍḭḭṛḛḛͼţḽẏẏ ẁḭḭţḥ ẏẏṓṓṵṵṛ ṓṓẁṇ ţḛḛṇααṇţ.