liveBook · Manning

LoginManager

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

LoginManager is the Flask-Login manager object that coordinates loading users and maintaining authenticated sessions.

A login route needs more than a password check: the application must also recognize the user on later requests. Flask-Login’s LoginManager coordinates that authentication state and loads users from persistent storage when needed. section 8.2.1

Create one LoginManager alongside the application’s other extensions, set `login_view` to `auth_bp.login`, and call `login_manager.init_app(app)` inside the `create_app()` factory. This keeps authentication setup with the application while the auth Blueprint owns the login route. section 8.2.1

Load a returning user

Register a `@login_manager.user_loader` function that accepts a user ID and queries the SQLAlchemy `User` model within a database session. The manager can then retrieve the stored user; password hashing remains a separate concern. section 8.2.1

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Shows factory initialization, login-route configuration, and a database-backed user loader; defers detailed database treatment.
Examples
Uses an auth Blueprint login route and a user-loader callback that queries a stored User by ID.
What it assumes
Assumes familiarity with Flask application factories and Blueprints; postpones detailed SQLAlchemy instruction.
What you can do after
Apply it via Flask-Login to initialize authentication and retrieve users from a database.
How it connects
Connects Flask-Login to application factories, Blueprints, SQLAlchemy user models, and password hashing.

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.