MCPcopy
hub / github.com/pallets/flask / login

Function login

examples/tutorial/flaskr/auth.py:85–109  ·  view source on GitHub ↗

Log in a registered user by adding the user id to the session.

()

Source from the content-addressed store, hash-verified

83
84@bp.route("/login", methods=("GET", "POST"))
85def login():
86 """Log in a registered user by adding the user id to the session."""
87 if request.method == "POST":
88 username = request.form["username"]
89 password = request.form["password"]
90 db = get_db()
91 error = None
92 user = db.execute(
93 "SELECT * FROM user WHERE username = ?", (username,)
94 ).fetchone()
95
96 if user is None:
97 error = "Incorrect username."
98 elif not check_password_hash(user["password"], password):
99 error = "Incorrect password."
100
101 if error is None:
102 # store the user id in a new session and return to the index
103 session.clear()
104 session["user_id"] = user["id"]
105 return redirect(url_for("index"))
106
107 flash(error)
108
109 return render_template("auth/login.html")
110
111
112@bp.route("/logout")

Callers

nothing calls this directly

Calls 5

redirectFunction · 0.90
url_forFunction · 0.90
flashFunction · 0.90
render_templateFunction · 0.90
get_dbFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…