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 Flask 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.
requirements.txt file in your project directory:
# 📁 requirements.txt ----- flask>=2.0.3 python-dotenv>=0.19.2 authlib>=1.0 requests>=2.27.1
Run the following command from your shell to enable these dependencies in your project:
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 APP_SECRET_KEY=
openssl rand -hex 32from your shell.
Next, set up your application. Create a
server.py file in your project directory - this file will contain your application logic.
Import all the libraries your application needs.
Load the configuration
.env file you made in the previous step.
Configure Authlib to handle your application's authentication with Auth0. To learn more about the configuration options available for Authlib's OAuth
register() method from their documentation.
In this example, you will add four routes to the application: login, callback, logout, and home.
When visitors to your app visit the
/login route, your application will route them to the Auth0 login page.
After your users log in with Auth0, your application will route them to the
/callback route. This route saves the session for the user and bypasses the need for them to login again when they return.
/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.
/ home route either renders an authenticated user's details or allows visitors to sign in.
Next, create the template file used in the home route (during
Create a new sub-directory in your project folder named
templates, and create
home.html in the directory. Paste the content from the right into that file.
To run your application, navigate to the root of your project directory and open a terminal. Run the following command:
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.