Flask Login
To start using Flask Login, it needs to be downloaded first:
$ pip install flask-login
The main Flask Login object is the LoginManager
object. Like the other Flask extensions, initialize the LoginManager
object in extensions.py
:
from flask.ext.login import LoginManager … login_manager = LoginManager()
There are some configuration options that need to be changed on the object:
login_manager.login_view = "main.login" login_manager.session_protection = "strong" login_manager.login_message = "Please login to access this page" login_manager.login_message_category = "info" @login_manager.user_loader def load_user(userid): from models import User return User.query.get(userid)
The preceding configuration values define which view should be treated as the login page and what the message to the user while logging in should look like. Setting the option session_protection
to strong
better protects against malicious users tampering with their cookies. When a tampered cookie is identified...