From 9ab0faf33b9247ae9d04eedec5d57d96d22f40a5 Mon Sep 17 00:00:00 2001 From: Daniel Maclaren Date: Wed, 3 Apr 2024 14:13:08 +0100 Subject: [PATCH] added session and logout abilities --- app.py | 18 +++++++++++----- templates/layout.html | 48 ++++++++++++++++++++----------------------- 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/app.py b/app.py index 5d49f45..bbad9bd 100644 --- a/app.py +++ b/app.py @@ -6,7 +6,7 @@ from werkzeug.security import check_password_hash, generate_password_hash app = Flask(__name__) - +app.secret_key = "test" app.config["SESSION_PERMANENT"] = False app.config["SESSION_TYPE"] = "filesystem" Session(app) @@ -30,6 +30,8 @@ def after_request(response): @app.route("/") def index(): + #user_id = session["user_id"] + return render_template("index.html") @app.route("/login", methods=["GET", "POST"]) @@ -57,13 +59,19 @@ def login(): ): return apology("Invalid username and/or password", 400) - #session["user_id"] = rows[0]["id"] + session["user_id"] = data[0][0] return redirect("/") elif request.method == "GET": return render_template("login.html") +@app.route("/logout") +def logout(): + session.clear() + #session.pop("user_id", None) + return render_template("login.html") + @app.route("/register", methods=["GET", "POST"]) def register(): """Register user""" @@ -78,11 +86,9 @@ def register(): elif request.form.get("password") != request.form.get("confirmation"): return apology("passwords must match", 400) - # if username is already taken rows = db.execute( "SELECT * FROM users WHERE username = ?", (request.form.get("username"),) ) - conn.commit() data = rows.fetchall() @@ -100,8 +106,10 @@ def register(): ) conn.commit() + new_user_id = new_user.lastrowid + - """session["user_id"] = new_user""" + session["user_id"] = new_user_id return redirect("/") diff --git a/templates/layout.html b/templates/layout.html index a844c84..7cd4f67 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -8,36 +8,32 @@ - {% block content %}{% endblock %} + CURL {% block content %}{% endblock %}