By Evan Sims
This tutorial demonstrates how to add user login to a Python web Application built with the Flask framework and Authlib OAuth library.We recommend that you log in to follow this quickstart with examples configured for your account.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.
- Domain
- Client ID
- Client Secret
If you download the sample from the top of this page, these details are filled out for you.
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 added to 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 add to 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 thereturnTo query parameter. The logout URL for your app must be added to 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 add to the Allowed Logout URLs field is
http://localhost:3000.Install dependencies
For the purposes of this example, we’ll be using the Authlib OAuth library and Flask. Begin by creating arequirements.txt file in your project directory:
pip install -r requirements.txt from your shell to make these dependencies available to your project.
Configure your .env file
Next, create an.env file in your project directory. This file will hold your client keys and other configuration details.
- Generate a suitable string for
APP_SECRET_KEYusingopenssl rand -hex 32from your shell.
Setup your application
Now you’re ready to start writing your application. Create aserver.py file in your project directory - this file will hold all of your application logic.
Begin by importing all the libraries your application will be making use of:
.env file you made in the previous step:
register() method from their documentation.
Setup your routes
For this demonstration, we’ll be adding 4 routes for your application: your login, callback, logout and home routes.Triggering authentication with /login
When visitors to your app visit the/login route, they’ll be redirected to Auth0 to begin the authentication flow.
Finalizing authentication with /callback
After your users finish logging in with Auth0, they’ll be returned to your application at the/callback route. This route is responsible for actually saving the session for the user, so when they visit again later, they won’t have to sign back in all over again.
Clearing a session with /logout
As you might expect, this route handles signing a user out from your application. It will clear the user’s session in your app, and briefly redirect to Auth0’s logout endpoint to ensure their session is completely clear, before they are returned to your home route (covered next.)There’s no place like /home
Last but not least, your home route will serve as a place to either render an authenticated user’s details, or offer to allow visitors to sign in.Server instantiation
Finally, you’ll need to add some small boilerplate code for Flask to actually run your app and listen for connections.Add templates
Now we just need to create the simple template files used in the routes about (duringrender_template() calls).
Create a new sub-directory in your project folder named templates, and create two files within: dashboard.html and home.html. You can paste the content from the two fields below into those files, respectfully:
Run your application
You’re ready to run your application! From your project directory, open a shell and use:http://localhost:3000.