Redirect logged-in users

This commit is contained in:
augustin64 2023-06-25 09:24:44 +02:00
parent c735b1e496
commit e4777cf7a6
2 changed files with 16 additions and 1 deletions

View File

@ -82,9 +82,9 @@ def add_user():
return render_template("auth/register.html", albums=get_all_albums(), user=current_user)
# Automatically close db after each request
@app.after_request
def after_request(response):
"""Automatically close db after each request"""
if ('db' in g) and (g.db is not None):
g.db.close()
return response

View File

@ -38,6 +38,19 @@ def login_required(view):
return wrapped_view
def anon_required(view):
"""View decorator that redirects authenticated users to the index."""
@functools.wraps(view)
def wrapped_view(**kwargs):
if g.user is not None:
return redirect(url_for("albums.index"))
return view(**kwargs)
return wrapped_view
def admin_required(view):
"""View decorator that redirects anonymous users to the login page."""
@ -72,6 +85,7 @@ def load_logged_in_user():
@bp.route("/register", methods=("GET", "POST"))
@anon_required
def register():
"""Register a new user.
Validates that the username is not already taken. Hashes the
@ -115,6 +129,7 @@ def register():
@bp.route("/login", methods=("GET", "POST"))
@anon_required
def login():
"""Log in a registered user by adding the user id to the session."""
if request.method == "POST":