From 2d566063636874ffcc3aef0558ec97f030830015 Mon Sep 17 00:00:00 2001 From: Daniel Maclaren Date: Tue, 2 Apr 2024 17:06:48 +0100 Subject: [PATCH] added the ability to log in --- app.py | 34 +++++++++++++++++++++++++++++++--- templates/login.html | 8 ++++---- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index 08d0115..5d49f45 100644 --- a/app.py +++ b/app.py @@ -29,12 +29,40 @@ def after_request(response): return response @app.route("/") -def home(): +def index(): return render_template("index.html") -@app.route("/login") +@app.route("/login", methods=["GET", "POST"]) def login(): - return render_template("login.html") + + session.clear() + + if request.method == "POST": + + if not request.form.get("username"): + return apology("must provide username", 400) + + elif not request.form.get("password"): + return apology("must provide password", 400) + + rows = db.execute( + "SELECT * FROM users WHERE username = ?", (request.form.get("username"),) + ) + conn.commit() + + data = rows.fetchall() + + if len(data) != 1 or not check_password_hash( + data[0][2], request.form.get("password") + ): + return apology("Invalid username and/or password", 400) + + #session["user_id"] = rows[0]["id"] + + return redirect("/") + + elif request.method == "GET": + return render_template("login.html") @app.route("/register", methods=["GET", "POST"]) def register(): diff --git a/templates/login.html b/templates/login.html index 87b17af..8f17570 100644 --- a/templates/login.html +++ b/templates/login.html @@ -7,12 +7,12 @@ {% block main %}
- - + +
- - + +