diff options
author | Frederick Yin <fkfd@fkfd.me> | 2021-10-27 22:03:56 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2021-10-27 22:03:56 +0800 |
commit | 106202912286b4b98ede620a83ae1dd89d42d225 (patch) | |
tree | 8bedd5e6edfcbbf611b728c17ff3866d56d33cac /jimbrella | |
parent | 0e482d22fc79e80eef0511326d93b1e17d453de5 (diff) |
Register new users
Diffstat (limited to 'jimbrella')
-rw-r--r-- | jimbrella/auth.py | 50 | ||||
-rw-r--r-- | jimbrella/web.py | 3 |
2 files changed, 53 insertions, 0 deletions
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")) diff --git a/jimbrella/web.py b/jimbrella/web.py index f9bc586..9db37ef 100644 --- a/jimbrella/web.py +++ b/jimbrella/web.py @@ -1,9 +1,12 @@ from flask import Flask from .admin import bp as admin_bp +from .auth import bp as auth_bp from .config import * app = Flask("jimbrella") +app.secret_key = FLASK_SECRET_KEY app.register_blueprint(admin_bp) +app.register_blueprint(auth_bp) if __name__ == "__main__": app.run() |