From 32e06e9d98ae2bcfcaa9709427b4e496520c28d4 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sun, 21 Jan 2024 15:24:33 +0000 Subject: [PATCH] Add option to logout --- main.py | 10 ++++++++++ templates/base.html | 3 +++ templates/flash_msg.html | 10 ++++++++++ templates/login.html | 2 +- templates/navbar.html | 1 + 5 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 templates/flash_msg.html diff --git a/main.py b/main.py index 2eb5400..48adcbd 100755 --- a/main.py +++ b/main.py @@ -85,6 +85,7 @@ def login_page() -> str | werkzeug.Response: return flask.render_template("login.html", error="Invalid credentials") expire_date = datetime.now() + timedelta(days=180) + flask.flash("Welcome back! You have successfully logged in.") response = flask.redirect(flask.session.get("next") or flask.url_for("dashboard")) response.set_cookie( @@ -99,5 +100,14 @@ def login_page() -> str | werkzeug.Response: return response +@app.route("/logout") +def logout() -> werkzeug.Response: + """Handle user logout by clearing the authentication cookie.""" + response = flask.redirect(flask.url_for("login_page")) + response.set_cookie("auth_token", "", expires=0) + flask.flash("You have been successfully logged out.", "info") + return response + + if __name__ == "__main__": app.run(host="0.0.0.0") diff --git a/templates/base.html b/templates/base.html index 7b1cbde..1e87fa5 100644 --- a/templates/base.html +++ b/templates/base.html @@ -16,7 +16,10 @@ {% block nav %}{{ navbar() }}{% endblock %} +
+{% include "flash_msg.html" %} {% block content %}{% endblock %} +
{% block scripts %}{% endblock %} diff --git a/templates/flash_msg.html b/templates/flash_msg.html new file mode 100644 index 0000000..673885c --- /dev/null +++ b/templates/flash_msg.html @@ -0,0 +1,10 @@ +{% with messages = get_flashed_messages() %} + {% if messages %} + {% for message in messages %} + + {% endfor %} + {% endif %} +{% endwith %} diff --git a/templates/login.html b/templates/login.html index 4eb9fff..bfc652e 100644 --- a/templates/login.html +++ b/templates/login.html @@ -13,7 +13,7 @@
- +
diff --git a/templates/navbar.html b/templates/navbar.html index a9253f0..762cae8 100644 --- a/templates/navbar.html +++ b/templates/navbar.html @@ -3,6 +3,7 @@ {% set pages = [ {"endpoint": "root_page", "label": "Home" }, {"endpoint": "login_page", "label": "Login" }, + {"endpoint": "logout", "label": "Logout" }, ] %} {% set project = "Auth" %}