Add option to logout

This commit is contained in:
Edward Betts 2024-01-21 15:24:33 +00:00
parent 49b228c1a5
commit 32e06e9d98
5 changed files with 25 additions and 1 deletions

10
main.py
View file

@ -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")

View file

@ -16,7 +16,10 @@
<body>
{% block nav %}{{ navbar() }}{% endblock %}
<div class="container mt-3">
{% include "flash_msg.html" %}
{% block content %}{% endblock %}
</div>
{% block scripts %}{% endblock %}
<script src="https://unpkg.com/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
</body>

10
templates/flash_msg.html Normal file
View file

@ -0,0 +1,10 @@
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for message in messages %}
<div class="alert alert-success alert-dismissible fade show" role="alert">
{{ message }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endfor %}
{% endif %}
{% endwith %}

View file

@ -13,7 +13,7 @@
<form method="POST">
<div class="mb-3">
<label for="username" class="form-label">username</label>
<input class="form-control" id="username" name="username">
<input class="form-control" id="username" name="username" value="{{ request.form.username }}">
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>

View file

@ -3,6 +3,7 @@
{% set pages = [
{"endpoint": "root_page", "label": "Home" },
{"endpoint": "login_page", "label": "Login" },
{"endpoint": "logout", "label": "Logout" },
] %}
{% set project = "Auth" %}