From b1177ae6e48410ec4919d27577dab960a266b2dd Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Thu, 19 Jan 2023 11:59:03 +0300 Subject: oauth2: Implement user registration. --- wqflask/wqflask/oauth2/routes.py | 34 ++++++++++++ .../wqflask/templates/oauth2/register_user.html | 61 ++++++++++++++++++++++ wqflask/wqflask/views.py | 1 + 3 files changed, 96 insertions(+) create mode 100644 wqflask/wqflask/templates/oauth2/register_user.html diff --git a/wqflask/wqflask/oauth2/routes.py b/wqflask/wqflask/oauth2/routes.py index deb540e8..83994284 100644 --- a/wqflask/wqflask/oauth2/routes.py +++ b/wqflask/wqflask/oauth2/routes.py @@ -1,4 +1,5 @@ """Routes for the OAuth2 auth system in GN3""" +import requests from urllib.parse import urljoin from pymonad.maybe import Just, Maybe, Nothing @@ -72,6 +73,39 @@ def logout(): return redirect("/") +@oauth2.route("/register-user", methods=["GET", "POST"]) +def register_user(): + if user_logged_in(): + next_endpoint=request.args.get("next", url_for("/")) + flash(("You cannot register a new user while logged in. " + "Please logout to register a new user."), + "alert-danger") + return redirect(next_endpoint) + + if request.method == "GET": + return render_template("oauth2/register_user.html") + + config = app.config + form = request.form + response = requests.post( + urljoin(config["GN_SERVER_URL"], "oauth2/register-user"), + data = { + "user_name": form.get("user_name"), + "email": form.get("email_address"), + "password": form.get("password"), + "confirm_password": form.get("confirm_password")}) + results = response.json() + if "error" in results: + error_messages = tuple( + f"{results['error']}: {msg.strip()}" + for msg in results.get("error_description").split("::")) + for message in error_messages: + flash(message, "alert-danger") + return redirect(url_for("oauth2.register_user")) + + flash("Registration successful! Please login to continue.", "alert-success") + return redirect(url_for("oauth2.login")) + @oauth2.route("/register-client", methods=["GET", "POST"]) @require_oauth2 def register_client(): diff --git a/wqflask/wqflask/templates/oauth2/register_user.html b/wqflask/wqflask/templates/oauth2/register_user.html new file mode 100644 index 00000000..0cdcd5ff --- /dev/null +++ b/wqflask/wqflask/templates/oauth2/register_user.html @@ -0,0 +1,61 @@ +{%extends "base.html"%} +{%block title%}Register New User{%endblock%} +{%block content%} +