What is Flask-Login and how does it work?

Answer

Flask-Login handles user session management for Flask applications. It manages logging in, logging out, and remembering users between sessions. Setup: login_manager = LoginManager(app); login_manager.login_view = 'auth.login'. Define the user loader: @login_manager.user_loader def load_user(user_id): return User.query.get(user_id). Login a user: login_user(user, remember=remember_me). Protect routes: @login_required — redirects to the login page if not authenticated. Get the current user: from flask_login import current_user. Logout: logout_user(). Flask-Login stores the user ID in the Flask session (signed cookie). Your User model must implement the UserMixin interface (is_authenticated, is_active, get_id()). It works best with session-based web apps; for API authentication, use JWT (Flask-JWT-Extended) instead.