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.
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.
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.
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.
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.
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:
# 📁 requirements.txt ----- authlib ~= 1.0 django ~= 4.0 python-dotenv ~= 0.19 requests ~= 2.27
Run the following command from your shell to make these dependencies available:
pip install -r requirements.txt
Next, create an
.env file in your project directory. This file will hold your client keys and other configuration details.
# 📁 .env ----- AUTH0_CLIENT_ID=YOUR_CLIENT_ID AUTH0_CLIENT_SECRET=YOUR_CLIENT_SECRET AUTH0_DOMAIN=YOUR_DOMAIN
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:
webappexample/settings.py file to review the
At the top of the file, add the
Next, beneath the
BASE_DIR definition, add the
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.
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.
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
/loginroute, 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
/callbackroute. This route saves the session for the user and bypasses the need for them to login again when they return.
/logoutroute 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.
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
/ routes to the correct handlers.
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 will contain template code to display the user's info if logged in or present them with a login button if logged out.
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
Your application should now be ready to open from your browser at http://localhost:3000.
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.
Excellent work! If you made it this far, you should now have login, logout, and user profile information running in your application.
This concludes our quickstart tutorial, but there is so much more to explore. To learn more about what you can do with Auth0, check out:
Did it work?
Any suggestion or typo?Edit on GitHub
Sign up for an or to your existing account to integrate directly with your own tenant.