from flask import Flask from flask.ext.login import LoginManager app = Flask(__name__) login_manager = LoginManager() def create_app(): login_manager.init_app(app) # ... other application setup ... return app
@login_manager.unauthorized_handler def unauthorized(): return redirect(url_for('login'))Here, we define a custom unauthorized handler that redirects users to the login view when they try to access a protected view without logging in. The flask.ext.login package is part of the Flask ecosystem and is installed as a dependency when installing Flask.