Flask-Login
Flask-Login is the extension that manages user authentication sessions in Flask. It tracks who is logged in, remembers them across requests, and provides helpers like login_user(), logout_user(), and the login_required decorator.
Flask
Authentication has two parts: verifying credentials (which you do yourself, e.g. checking a hashed password) and remembering that the user is logged in. Flask-Login handles the second part by storing the user id in the session and reloading the user on each request.
You set up a LoginManager, give it a user_loader callback that turns an id back into a user object, and make your User model implement the required interface (usually by mixing in UserMixin). After verifying a password you call login_user(user); to log out you call logout_user().
Flask-Login also gives you current_user (the logged-in user or an anonymous proxy) and the @login_required decorator that redirects anonymous visitors to your login page.
Syntax
from flask_login import login_user, logout_user, login_requiredExamples
Python
from flask_login import LoginManager, UserMixin, login_user
login_manager = LoginManager(app)
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
@login_manager.user_loader
def load_user(user_id):
return db.session.get(User, int(user_id))
# after checking the password:
# login_user(user)