1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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"))
|