Flask-Login requires a user model to represent the users in your application. This model should implement certain methods that Flask-Login uses to manage user sessions. The most important methods are:
is_authenticated
: Returns True
if the user is authenticated, False
otherwise.is_active
: Returns True
if the user’s account is active, False
otherwise.is_anonymous
: Returns True
for anonymous users, False
for authenticated users.get_id()
: Returns a unique identifier for the user, usually a string.The LoginManager
class is the heart of Flask-Login. It manages the user sessions and provides methods for logging users in and out. You need to initialize the LoginManager
and attach it to your Flask application.
Flask-Login uses Flask’s session mechanism to store information about the logged-in user. When a user logs in, Flask-Login stores the user’s ID in the session. On subsequent requests, Flask-Login retrieves the user ID from the session and loads the corresponding user object.
The most common use case is to provide a login and logout functionality for users. Users can enter their credentials, and if they are valid, they are logged in. They can then access protected pages. When they log out, their session is terminated.
Flask-Login allows you to protect certain routes so that only authenticated users can access them. This is useful for pages that contain sensitive information or perform actions that should only be available to authorized users.
You can integrate user registration with Flask-Login. After a user registers, they can log in using their newly created credentials.
pip install flask flask-login
LoginManager
:from flask import Flask
from flask_login import LoginManager
app = Flask(__name__)
app.secret_key = 'your_secret_key'
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
from flask_login import UserMixin
# Assume we have a simple in-memory user database
users = {
'user1': {'password': 'password1'}
}
class User(UserMixin):
def __init__(self, id):
self.id = id
@staticmethod
def get(user_id):
if user_id in users:
return User(user_id)
return None
# Callback to reload the user object
@login_manager.user_loader
def load_user(user_id):
return User.get(user_id)
from flask import render_template, request, redirect, url_for
from flask_login import login_user
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
user = User.get(username)
if user and users[username]['password'] == password:
login_user(user)
return redirect(url_for('protected'))
else:
return 'Invalid credentials'
return render_template('login.html')
from flask_login import login_required
@app.route('/protected')
@login_required
def protected():
return 'This is a protected page'
from flask_login import logout_user
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
login.html
)<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<form method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br>
<input type="submit" value="Login">
</form>
</body>
</html>
Flask-Login uses Flask’s session mechanism, which requires a secret key. If you forget to set the secret key, Flask-Login will not work correctly. Make sure to set a strong and unique secret key in your application.
The user_loader
callback is essential for Flask-Login to load the user object from the user ID stored in the session. If this callback is not implemented correctly, Flask-Login will not be able to retrieve the user object.
In the code examples, we used plain text passwords for simplicity. In a real-world application, you should always hash passwords using a strong hashing algorithm like bcrypt.
Use a strong hashing algorithm like bcrypt to store passwords securely. Here is an example of how to use bcrypt:
import bcrypt
password = 'password1'.encode('utf-8')
hashed = bcrypt.hashpw(password, bcrypt.gensalt())
# Verify password
if bcrypt.checkpw(password, hashed):
print('Password is correct')
else:
print('Password is incorrect')
Implement proper error handling in your login and registration routes. Display meaningful error messages to users when they enter invalid credentials or encounter other issues.
Set appropriate session expiration times to ensure that user sessions are terminated after a certain period of inactivity. You can use Flask’s session configuration to set the session lifetime.
Flask-Login is a powerful and easy-to-use extension for Flask that simplifies the process of implementing user authentication in web applications. By understanding the core concepts, typical usage scenarios, and best practices, you can effectively use Flask-Login to build secure and user-friendly web applications. Remember to handle common pitfalls such as setting the secret key and using proper password hashing to ensure the security of your application.