Docs

Python

View on Github

Python

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

This tutorial demonstrates how to add user login to a Python web Application built with the Flask framework. 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 2.7, 3.0 and up | Flask 0.10.1 and up | Python-dotenv 0.6.5 and up | Requests 2.3.0 and up | Flask-oauthlib 0.9.4 and up | Six 1.10.0 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:

  • Domain
  • Client ID
  • Client Secret

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.

The callback URL for your app must be whitelisted in the Allowed Callback URLs field in your Application Settings. If this field is not set, users will be unable to log in to the application and will get an error.

If you are following along with the sample project you downloaded from the top of this page, the callback URL you need to whitelist in the Allowed Callback URLs field is http://localhost:3000/callback.

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.

The logout URL for your app must be whitelisted in the Allowed Logout URLs field in your Application Settings. If this field is not set, users will be unable to log out from the application and will get an error.

If you are following along with the sample project you downloaded from the top of this page, the logout URL you need to whitelist in the Allowed Logout URLs field is http://localhost:3000.

Configure Flask to Use Auth0

Add the Dependencies

This example uses Flask and the Authlib OAuth library.

Add the following dependencies to your requirements.txt and run pip install -r requirements.txt.

Initialize Authlib

Create a file named server.py, and instantiate an application with your client keys, scopes, and OAuth endpoints.

Add the Callback Handler

This handler exchanges the code that Auth0 sends to the callback URL for an Access Token and an ID Token.

The Access Token will be used to call the /userinfo endpoint to get the user profile. After the user information is obtained, store then in the flask session.

Trigger Authentication

Add a /login route that uses the Authlib client instance to redirect the user to the login page.

Create a home.html file in a /template folder. Add a link to the /login route.

Check if the user is authenticated

Add the following decorator to your Flask app. Use it to decorate methods that require authentication.

You should import wraps first, adding the following line to your server.py file: from functools import wraps.

Display User Information

Add a /dashboard route to server.py that will render the user information stored in the Flask session.

Decorate it with @requires_auth. It will only be accessible if the user has been authenticated.

Add a dashboard.html file in a /template folder to display the user information.

Add a link to allow users to Log Out.

Logout

To log the user out, you have to clear the data from the session, and redirect the user to the Auth0 logout endpoint. You can find more information about this in our documentation logout documentation.

The redirect URL needs to be in the list of Allowed Logout URLs in the settings section of the application, For more information, see Redirect Users After Logout.

Use Auth0 for FREE