login_required (Flask-Login)

login_required is a Flask-Login decorator that protects a view so only authenticated users can reach it. Anonymous visitors are redirected to the configured login page instead of seeing the protected content.

Flask

Place @login_required directly under @app.route to gate a page behind a login. When an anonymous user requests it, Flask-Login intercepts the request and redirects them to login_manager.login_view, often remembering the original URL so it can send them back after they sign in.

It only checks that someone is logged in, not what they are allowed to do. For role- or permission-based access ('only admins'), add your own check inside the view or use an extension like Flask-Principal.

Decorator order matters: @app.route must be on top and @login_required below it, so the login check wraps the actual view function.

Syntax

@app.route("/secret")
@login_required
def secret(): ...

Examples

Python

from flask_login import login_required

login_manager.login_view = "login"

@app.route("/account")
@login_required
def account():
    return "Your private account page"

Learn it in a lesson

See also

Back to the Dictionary