Authentication in Flask Apps: Logging in with Flask (+16, -2)

__init__.py (+15, -1)

From: curriculum/section13/lectures/03_logging_in_users/start/app/__init__.py

To: curriculum/section13/lectures/03_logging_in_users/end/app/__init__.py

            
            index 506c02e..c145ae8 100644
--- a/curriculum/section13/lectures/03_logging_in_users/start/app/__init__.py
+++ b/curriculum/section13/lectures/03_logging_in_users/end/app/__init__.py
@@ -29,8 +29,17 @@ def protected():
     return render_template("protected.html")
 
 
-@app.route("/login")
+@app.route("/login", methods=["GET", "POST"])
 def login():
+    if request.method == "POST":
+        email = request.form.get("email")
+        password = request.form.get("password")
+
+        if users.get(email) == password:
+            session["email"] = email
+            return redirect(url_for("protected"))
+        else:
+            abort(401)
     return render_template("login.html")
 
 
@@ -48,3 +57,8 @@ def signup():
         flash("Successfully signed up.")
         return redirect(url_for("login"))
     return render_template("signup.html")
+
+
+@app.errorhandler(401)
+def auth_error():
+    return "Not authorized"
        

login.html (+1, -1)

From: curriculum/section13/lectures/03_logging_in_users/start/app/templates/login.html

To: curriculum/section13/lectures/03_logging_in_users/end/app/templates/login.html

            
            index d86588a..1edb426 100644
--- a/curriculum/section13/lectures/03_logging_in_users/start/app/templates/login.html
+++ b/curriculum/section13/lectures/03_logging_in_users/end/app/templates/login.html
@@ -2,7 +2,7 @@
 <form method="POST">
   <label>
     E-mail
-    <input type="email" name="email" />
+    <input type="email" name="email" value="{{ email }}" />
   </label>
   <label>
     Password