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.
Previous
How do you implement file uploads in FastAPI?
Next
How do you deploy FastAPI to production?
More FastAPI / Flask Questions
View all →- Intermediate How do you implement JWT authentication in FastAPI?
- Intermediate How does SQLAlchemy async work with FastAPI?
- Intermediate What is Flask's application factory pattern?
- Intermediate How do you implement background tasks in FastAPI?
- Intermediate What is Pydantic v2 and what changed from v1?