auth.py
This file
Path
examples/tutorial/flaskr/auth.py
116 lines · 3.2 KiB · Python
Last changed by
Merge pull request #3246 from pallets/reorder-importsDavid Lordbefore adoption3db4697
History of this fileTake it
The walk stopped after 200 commits: lines older than be46acb point at it.
add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 1–2why, one step back1import functools2Merge pull request #3246 from pallets/reorder-importsDavid Lordbefore adoption3db4697
lines 3–12why, one step back3from flask import Blueprint4from flask import flash5from flask import g6from flask import redirect7from flask import render_template8from flask import request9from flask import session10from flask import url_for11from werkzeug.security import check_password_hash12from werkzeug.security import generate_password_hashadd changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 13–15why, one step back1314from flaskr.db import get_db15Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 16why, one step back16bp = Blueprint("auth", __name__, url_prefix="/auth")add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 17–20why, one step back171819def login_required(view):20 """View decorator that redirects anonymous users to the login page."""Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 21why, one step back21add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 22–24why, one step back22 @functools.wraps(view)23 def wrapped_view(**kwargs):24 if g.user is None:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 25why, one step back25 return redirect(url_for("auth.login"))add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 26–35why, one step back2627 return view(**kwargs)2829 return wrapped_view303132@bp.before_app_request33def load_logged_in_user():34 """If a user id is stored in the session, load the user object from35 the database into ``g.user``."""Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 36why, one step back36 user_id = session.get("user_id")add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 37–40why, one step back3738 if user_id is None:39 g.user = None40 else:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 41–43why, one step back41 g.user = (42 get_db().execute("SELECT * FROM user WHERE id = ?", (user_id,)).fetchone()43 )add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 44–45why, one step back4445Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 46why, one step back46@bp.route("/register", methods=("GET", "POST"))add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 47–52why, one step back47def register():48 """Register a new user.4950 Validates that the username is not already taken. Hashes the51 password for security.52 """Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 53–55why, one step back53 if request.method == "POST":54 username = request.form["username"]55 password = request.form["password"]add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 56–59why, one step back56 db = get_db()57 error = None5859 if not username:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 60why, one step back60 error = "Username is required."add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
line 61why, one step back61 elif not password:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 62–67why, one step back62 error = "Password is required."63 elif (64 db.execute("SELECT id FROM user WHERE username = ?", (username,)).fetchone()65 is not None66 ):67 error = "User {0} is already registered.".format(username)add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 68–72why, one step back6869 if error is None:70 # the name is available, store it in the database and go to71 # the login page72 db.execute(Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 73–74why, one step back73 "INSERT INTO user (username, password) VALUES (?, ?)",74 (username, generate_password_hash(password)),add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 75–76why, one step back75 )76 db.commit()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 77why, one step back77 return redirect(url_for("auth.login"))add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 78–80why, one step back7879 flash(error)80Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 81why, one step back81 return render_template("auth/register.html")add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 82–83why, one step back8283Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 84why, one step back84@bp.route("/login", methods=("GET", "POST"))add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 85–86why, one step back85def login():86 """Log in a registered user by adding the user id to the session."""Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 87–89why, one step back87 if request.method == "POST":88 username = request.form["username"]89 password = request.form["password"]add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 90–92why, one step back90 db = get_db()91 error = None92 user = db.execute(Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 93why, one step back93 "SELECT * FROM user WHERE username = ?", (username,)add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 94–96why, one step back94 ).fetchone()9596 if user is None:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 97–99why, one step back97 error = "Incorrect username."98 elif not check_password_hash(user["password"], password):99 error = "Incorrect password."add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 100–103why, one step back100101 if error is None:102 # store the user id in a new session and return to the index103 session.clear()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 104–105why, one step back104 session["user_id"] = user["id"]105 return redirect(url_for("index"))add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 106–108why, one step back106107 flash(error)108Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 109why, one step back109 return render_template("auth/login.html")add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 110–111why, one step back110111Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 112why, one step back112@bp.route("/logout")add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 113–115why, one step back113def logout():114 """Clear the current session, including the stored user id."""115 session.clear()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 116why, one step back116 return redirect(url_for("index"))