54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
import functools
|
|
|
|
from flask import Blueprint
|
|
from flask import g
|
|
from flask import redirect
|
|
from flask import url_for
|
|
from flask import session
|
|
|
|
from capsulflask.db import get_model
|
|
|
|
bp = Blueprint("auth", __name__, url_prefix="/auth")
|
|
|
|
def account_required(view):
|
|
"""View decorator that redirects anonymous users to the login page."""
|
|
|
|
@functools.wraps(view)
|
|
def wrapped_view(**kwargs):
|
|
if session.get("account") is None:
|
|
return redirect(url_for("auth.login"))
|
|
|
|
return view(**kwargs)
|
|
|
|
return wrapped_view
|
|
|
|
|
|
@bp.route("/register", methods=("GET", "POST"))
|
|
def register():
|
|
|
|
if request.method == "POST":
|
|
email = request.form["email"]
|
|
model = get_model()
|
|
error = None
|
|
|
|
if not email:
|
|
error = "Email is required."
|
|
elif (
|
|
model.
|
|
):
|
|
error = f"User {username} is already registered."
|
|
|
|
if error is None:
|
|
# the name is available, store it in the database and go to
|
|
# the login page
|
|
db.execute(
|
|
"INSERT INTO user (username, password) VALUES (?, ?)",
|
|
(username, generate_password_hash(password)),
|
|
)
|
|
db.commit()
|
|
return redirect(url_for("auth.login"))
|
|
|
|
flash(error)
|
|
|
|
return render_template("auth/register.html")
|