Add Login to Your Python Flask 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 Flask application using the Authlib SDK.


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.


Install dependencies

Create a requirements.txt file in your project directory:

# 📁 requirements.txt -----


Was this helpful?


Run the following command from your shell to enable these dependencies in your project:

pip install -r requirements.txt

Was this helpful?


Configure your .env file

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

# 📁 .env -----


Was this helpful?

  • Generate a string for APP_SECRET_KEY using openssl rand -hex 32 from your shell.

Setup your application

Next, set up your application. Create a 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.


Setup your routes

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.

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.

Your / home route either renders an authenticated user's details or allows visitors to sign in.


Add templates

Next, create the template file used in the home route (during render_template() calls).

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.


Run your application

To run your application, navigate to the root of your project directory and open a terminal. Run the following command:


Was this helpful?


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.

Next Steps

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:

  • Auth0 Dashboard - Learn how to configure and manage your Auth0 tenant and applications
  • auth0-python SDK - Explore the SDK used in this tutorial more fully
  • Auth0 Marketplace - Discover integrations you can enable to extend Auth0’s functionality

Did it work?

Any suggestion or typo?

Edit on GitHub
Sign Up

Sign up for an or to your existing account to integrate directly with your own tenant.