How to Use Flask Built-In Authentication System

In this Flask article we want to learn How to Use Flask Built-In Authentication System, Flask is popular web framework for building web applications in Python. one of its best features is its built-in authentication system, which can be used to manage user accounts, access control, and authentication workflows. in this article we want to talk how to use Flask authentication system to build secure login page for your web application.

 

 

How to Use Flask Built-In Authentication System

First of all we need to install Flask, Flask-Login and SQLAlchemy

 

 

Now we need to import our required modules from Flask and Flask-Login, flask_login module provides us with necessary tools for authentication and authorization in our Flask application.

 

 

After that we need to create new instance of the Flask class and set secret key:

 

 

Next step is to define the User model. User model is responsible for storing user information, such as usernames, passwords, and email addresses. we will use SQLAlchemy ORM to create and interact with our database, and in here we are using sqlite database.

In the above code we have defined a User class that extends from db.Model. User class has four properties, id, username, email and password. we have also defined two methods: set_password() and check_password(), which will be used to hash and check passwords.

 

 

Next step is to initialize Login Manager. Login Manager is responsible for managing the user session, logging users in and out and handling authentication, in this code we have initialized Login Manager and set the login view to login. we also defined user_loader function that returns the user associated with given user_id.

 

 

Now that we have the User model and Login Manager set up, we can create the login and logout routes.

 

 

 

This is our user signup code in Flask Authentication

 

 

 

So now let’s create the complete code, this the code for app.py file 

In the above we have defined User model, initialized database and Login Manager, and creates login, logout, index and signup routes. we have also defined user_loader function, which is used by the Login Manager to load the user associated with a given user_id.

login route handles the authentication process, checking user credentials and logging them in if they are valid. logout route logs the user out of the application. index route requires the user to be logged in to access it. and lastly signup route handles the creation of new user accounts.

 

 

Now these our html files

 

base.html

This is the base template that all other templates extends from that. it defines the overall structure of the HTML document including navbar, container and JavaScript libraries. it also defines content block that can be overridden in child templates. {% if current_user.is_authenticated %} block displays different links in the navbar depending on whether the user is authenticated or not.

 

 

This is login.html

 

 

 

This is signup.html

 

 

This is index.html

 

 

This is our Flask Project Structrure,

Flask Project Structure
Flask Project Structure

 

 

Also make sure that you have created your table model in the SQLite database, for doing that first you need to navigate to the project folder and open flask shell in the terminal.

 

 

After that you can run this command, it will creates users table model in your SQLite database.

 

 

Run the project and this will be the pages 

 

Signup Page

How to Use Flask Built-In Authentication System
How to Use Flask Built-In Authentication System

 

 

 

Flask Login Page

How to Use Flask Built-In Authentication System
How to Use Flask Built-In Authentication System

 

 

 

This is Index Page

How to Use Flask Built-In Authentication System
How to Use Flask Built-In Authentication System

 

 

Learn More on Python Flask

Leave a Comment