Allowing users to log in to your app is one of the most common features you’ll add to your web application. This article will cover how to add authentication to your Flask app with the Flask-Login package.
We’re going to build some sign-up and login pages that allow users to log in and access protected pages that users who aren’t logged in can’t see. We’ll grab information from the user model and display it on our protected pages when the user logs in to simulate what a profile would look like.
We will cover the following in this article:
The source code for this project is available on GitHub.
Here is a diagram to provide a sense of what your project’s file structure will look like once you have completed the tutorial:
.
└── flask_auth_app
└── project
├── __init__.py # setup our app
├── auth.py # the auth routes for our app
├── db.sqlite # our database
├── main.py # the non-auth routes for our app
├── models.py # our user model
└── templates
├── base.html # contains common layout and links
├── index.html # show the home page
├── login.html # show the login form
├── profile.html # show the profile page
└── signup.html # show the signup form
As we progress through the tutorial, we will create these directories and files.
#flask #python #web-development #developer