From f6788a9586da9eb885173bf8e172fc8cb3714f6a Mon Sep 17 00:00:00 2001 From: Corwin Kerr Date: Sun, 2 Apr 2023 13:23:10 -0400 Subject: [PATCH 01/16] Enable login page with redirect to existing url --- signac_dashboard/dashboard.py | 23 ++++++++++++++++------- signac_dashboard/templates/layout.html | 2 ++ signac_dashboard/templates/login.html | 25 +++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 signac_dashboard/templates/login.html diff --git a/signac_dashboard/dashboard.py b/signac_dashboard/dashboard.py index 827b1ed..cd33a6b 100644 --- a/signac_dashboard/dashboard.py +++ b/signac_dashboard/dashboard.py @@ -582,17 +582,26 @@ def page_not_found(error): @dashboard.login_manager.unauthorized_handler def unauthorized_handler(): - return self._render_error("Access token is required.") + request_url = request.url + if "module" not in request_url: + session['redirect_url'] = request_url + return render_template('login.html') - @dashboard.app.route("/login") + @dashboard.app.route("/login", methods = ['GET', 'POST']) def login(): - provided_token = request.args.get("token") + if request.method == 'GET': + provided_token = request.args.get("token") + elif request.method == 'POST': + provided_token = request.form['token'] + else: + provided_token = None if provided_token == self.config["ACCESS_TOKEN"]: user = User(provided_token) flask_login.login_user(user) - return redirect("/") - - return self._render_error("Invalid token") + redirect_url = session.pop('redirect_url', '/') + return redirect(redirect_url) + else: + return render_template('login.html') @dashboard.app.route("/favicon.ico") @flask_login.login_required @@ -678,7 +687,7 @@ def _run(args): if self.config["ACCESS_TOKEN"] is not None: print( - f"To access this server, connect to: " + f"To access this server, connect to:\n" f"http://{self.config['HOST']}:{self.config['PORT']}/" f"login?token={self.config['ACCESS_TOKEN']}" ) diff --git a/signac_dashboard/templates/layout.html b/signac_dashboard/templates/layout.html index 23fc27f..f37850b 100644 --- a/signac_dashboard/templates/layout.html +++ b/signac_dashboard/templates/layout.html @@ -118,6 +118,7 @@

Modules