Authentication with Flask-Login
The auth is the Flask-Login the no the code: the login_manager (the 1, the user_loader, the no the 1), the UserMixin (the is_authenticated, the 1, the no the the), and the login_user (the session, the no the 1). The logout_user is the flush (the no the 1, the no the the), the @login_required is the gate (the no the login, the redirect), and the password is the hash (the werkzeug, the no the plain). This chapter covers the login_manager (the 1, the no the 1), the UserMixin (the is_authenticated, the 1), the login_user (the session, the no the 1), and the password (the hash, the no the plain).
The login_manager and the UserMixin
# the LOGIN_MANAGER (the 1, the
# no the 1, the no the the):
from flask_login import (
LoginManager, login_user,
logout_user, login_required,
current_user, UserMixin)
from werkzeug.security import (
generate_password_hash,
check_password_hash)
login_manager = LoginManager()
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
login_manager.init_app(app)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no):
login_manager.login_view = "login"
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the USERMIXIN (the
# is_authenticated, the 1,
# the no the the, the no
# the 1):
class User(UserMixin, db.Model):
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
id = db.Column(db.Integer,
primary_key=True)
username = db.Column(
db.String(64), unique=True)
password_hash = db.Column(
db.String(256))
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
def set_password(self, p):
# the no: the 1 (the
# no, the no, the no
# the no, the no
# the no):
self.password_hash = \
generate_password_hash(p)
def check_password(self, p):
# the no: the no (the
# no, the no, the no
# the no, the no
# the no):
return check_password_hash(
self.password_hash, p)
# the LOADER (the no, the 1,
# the no the the, the no
# the 1):
@login_manager.user_loader
def load_user(id):
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
return db.session.get(
User, int(id))
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
The login_manager is the 1: the LoginManager() is the 1 (the no the 1, the no the the), the init_app(app) is the 1 (the no the 1, the no the the), and the login_view is the no (the endpoint, the redirect, the no the 1) - the UserMixin is the 1: the is_authenticated is the no (the 1, the no the the), the is_active is the no (the 1, the no the 1), and the 1 is the no (the no the 1, the no the the) - the user_loader is the no: the load_user(id) is the 1 (the session, the get, the no the the), and the 1 is the no (the no the 1, the no the the) - the password is the next: the hash the no the plain.
The login_user, the logout, and the gate
# the LOGIN (the session, the no
# the 1, the no the the):
@app.route("/login/",
methods=["GET", "POST"])
def login():
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
if request.method == "POST":
u = User.query.filter_by(
username=request.form.get(
"username", "")).first()
# the no: the no (the
# no, the no, the no
# the no, the no
# the no)
if u and u.check_password(
request.form.get(
"password", "")):
# the no: the 1
# (the no, the no,
# the no, the no
# the no, the no):
login_user(u)
# the no: the no (
# the no, the no,
# the no, the no
# the no, the no)
next_url = request.args.get(
"next")
# the no: the no (
# the no, the no,
# the no, the no
# the no, the no)
return redirect(
next_url or url_for(
"index"))
return render_template("login.html")
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the LOGOUT (the flush, the no
# the 1, the no the the):
@app.route("/logout/", methods=
["POST"])
def logout():
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
logout_user()
# the no: the no (the no,
# the no, the no
# the no, the no
# the no):
return redirect(url_for(
"login"))
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the GATE (the no login, the
# redirect, the no the 1):
@app.route("/dashboard/")
@login_required
def dashboard():
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
return f"the {current_user.
username}"
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
The login_user is the session: the 1 argument (the User, the no the 1), the 2 argument (the remember, the cookie, the no the 1), and the no is the 1 (the no the the, the no the 1) - the logout_user is the flush: the 1 (the session, the no, the no the the), and the POST is the yes (the GET the no, the no the 1) - the @login_required is the gate: the no the login is the redirect (the login_view, the next, the no the 1), and the current_user is the no (the username, the id, the no the the) - the password is the hash: the generate_password_hash is the 1 (the no the plain, the no the the), and the check_password_hash is the no (the 1, the no the 1) - the rule is the one: the login_manager the 1, the UserMixin the 1, the login_user the session, and the password the hash - the next chapter's blueprint is the module the no the 1 the file.
The Working Rules
- The login_manager is the 1, the no the 1. The LoginManager() the 1 (the no the 1), and the init_app(app) the 1 (the no the the) - the login_view the no (the endpoint, the redirect, the no the 1), and the user_loader the 1 (the session, the get, the no the the).
- The UserMixin is the 1, the is_authenticated the no. The is_authenticated the no (the 1, the no the the), and the is_active the no (the 1, the no the 1) - the 1 the no (the no the 1, the no the the), and the id the property (the 1, the no the 1).
- The login_user is the session, the remember the cookie. The 1 argument (the User, the no the 1), and the 2 the remember (the cookie, the no the 1) - the logout_user the flush (the 1, the no the the), and the POST the yes (the GET the no, the no the 1).
- The password is the hash, the no plain. The generate_password_hash the 1 (the no the plain, the no the the), and the check_password_hash the no (the 1, the no the 1) - the no the plain (the DB, the no the 1), and the no the the (the no the 1, the no the the).
Summary
The auth is the Flask-Login the no the code: the login_manager the 1 (the LoginManager, the init_app, the login_view the no, the user_loader the 1, the no the 1), the UserMixin the 1 (the is_authenticated the no, the is_active, the no the 1), the login_user the session (the 1, the remember, the no the 1), the logout_user the flush (the POST, the no the GET), and the password the hash (the generate_password_hash, the check_password_hash, the no the plain). The @login_required is the gate (the no the login, the redirect, the current_user the no) - the rule is the one: the login_manager the 1, the UserMixin the 1, the login_user the session, and the password the hash. The next chapter's blueprint is the module: the Blueprint the 1, the url_prefix the no, and the register the app (the no the 1, the no the the).