From 106202912286b4b98ede620a83ae1dd89d42d225 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Wed, 27 Oct 2021 22:03:56 +0800 Subject: Register new users --- jimbrella/auth.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 jimbrella/auth.py (limited to 'jimbrella/auth.py') diff --git a/jimbrella/auth.py b/jimbrella/auth.py new file mode 100644 index 0000000..ca0f64a --- /dev/null +++ b/jimbrella/auth.py @@ -0,0 +1,50 @@ +from flask import Blueprint, request, render_template, redirect, url_for +from werkzeug.security import generate_password_hash, check_password_hash +from .users import Users +from .exceptions import UsernameTakenError +from .config import * + +bp = Blueprint("auth", __name__, url_prefix="/") +db = Users(USERS_PATH) + + +@bp.route("/login", methods=["GET", "POST"]) +def login(): + if request.method == "GET": + return render_template("auth.html", action="login") + # validate login information + + username = request.form.get("username") + password = request.form.get("password") + if not all([username, password]): + return render_template( + "auth.html", + action="login", + error="Please fill in both the username and password.", + ) + + +@bp.route("/register", methods=["GET", "POST"]) +def register(): + if request.method == "GET": + return render_template("auth.html", action="register") + + username = request.form.get("username") + password = request.form.get("password") + if not all([username, password]): + return render_template( + "auth.html", + action="register", + error="Please fill in both the username and password.", + ) + + try: + db.register(username, generate_password_hash(password), "en-US") + except UsernameTakenError as e: + return render_template( + "auth.html", + action="register", + error=e.message, + ) + + return redirect(url_for("admin.index")) -- cgit v1.2.3