How To Make Login Page Using Flask-login

Today I want to share with you my experience and knowledge on how to create a login page using Flask-Login. As a web developer, I have found Flask-Login to be a powerful and reliable tool for handling user authentication and session management in Flask applications.

Flask-Login is an extension that provides user session management for Flask applications. It handles the common tasks of logging in, logging out, and remembering user sessions. By using Flask-Login, you can easily add authentication and authorization to your Flask application.

To get started, you will need to install Flask-Login by running the following command in your terminal:

pip install flask-login

Once Flask-Login is installed, you can import and initialize it in your Flask application. Here’s an example:

from flask import Flask
from flask_login import LoginManager

app = Flask(__name__)
login_manager = LoginManager(app)

The LoginManager class is the core of Flask-Login. It is used to handle user sessions, login, and logout functionality. You can create an instance of LoginManager and pass in your Flask application as the argument.

Next, you will need to create a user model for your application. This model represents a user in your database and stores information such as email and password. You can use any database management system, such as SQLAlchemy or MongoDB, to store your user data. Here’s an example of a user model:

from flask_login import UserMixin

class User(UserMixin):
def __init__(self, id):
self.id = id
self.email = None
self.password = None

def set_password(self, password):
self.password = generate_password_hash(password)

def check_password(self, password):
return check_password_hash(self.password, password)

The UserMixin class provided by Flask-Login includes common methods and properties needed for user authentication. In this example, the User class has an id, email, and password attribute. It also has methods to set and check the password.

Once you have your user model set up, you can implement the login and registration views in your Flask application. Here’s an example:

from flask import render_template, redirect, url_for
from flask_login import login_user, logout_user, login_required

@app.route('/login', methods=['GET', 'POST'])
def login():
# Handle the login form submission
if request.method == 'POST':
email = request.form['email']
password = request.form['password']

# Check if the user exists and the password is correct
user = User.query.filter_by(email=email).first()
if user and user.check_password(password):
login_user(user)
return redirect(url_for('dashboard'))

# Render the login page
return render_template('login.html')

@app.route('/logout')
@login_required
def logout():
# Log the user out and redirect to the login page
logout_user()
return redirect(url_for('login'))

In this example, we have a /login route that handles both GET and POST requests. When a user submits the login form, we check if the email and password match a user in the database. If they do, we call the login_user() function to log the user in and redirect them to the dashboard page.

The /logout route is decorated with the @login_required decorator, which ensures that only authenticated users can access the route. When a user visits this route, we call the logout_user() function to log them out and redirect them to the login page.

Finally, you will need to create the login and registration templates for your application. These templates will typically include forms for the user to enter their email and password. Here’s an example of a login template:

{% extends 'base.html' %}

{% block content %}

Login




{% endblock %}

Once you have everything set up, you can start testing your login page. Run your Flask application and navigate to the login URL. Enter your email and password, and if everything is set up correctly, you should be redirected to the dashboard page.

Conclusion

Creating a login page using Flask-Login is a straightforward process that can greatly enhance the security and user experience of your Flask application. By utilizing the power of Flask-Login, you can easily handle user authentication and session management. I hope this article has provided you with a good understanding of how to implement a login page using Flask-Login.

If you want to dive deeper into Flask-Login, check out the official documentation for more details and advanced usage.