|
| 1 | +import functools |
| 2 | + |
| 3 | +from flask import Blueprint |
| 4 | +from flask import flash |
| 5 | +from flask import g |
| 6 | +from flask import redirect |
| 7 | +from flask import render_template |
| 8 | +from flask import request |
| 9 | +from flask import session |
| 10 | +from flask import url_for |
| 11 | +from werkzeug.security import check_password_hash |
| 12 | +from werkzeug.security import generate_password_hash |
| 13 | + |
| 14 | +from flaskr.db import get_db |
| 15 | + |
| 16 | +bp = Blueprint("auth", __name__, url_prefix="/auth") |
| 17 | + |
| 18 | + |
| 19 | +def login_required(view): |
| 20 | + """View decorator that redirects anonymous users to the login page.""" |
| 21 | + |
| 22 | + @functools.wraps(view) |
| 23 | + def wrapped_view(**kwargs): |
| 24 | + if g.user is None: |
| 25 | + return redirect(url_for("auth.login")) |
| 26 | + |
| 27 | + return view(**kwargs) |
| 28 | + |
| 29 | + return wrapped_view |
| 30 | + |
| 31 | + |
| 32 | +@bp.before_app_request |
| 33 | +def load_logged_in_user(): |
| 34 | + """If a user id is stored in the session, load the user object from |
| 35 | + the database into ``g.user``.""" |
| 36 | + user_id = session.get("user_id") |
| 37 | + |
| 38 | + if user_id is None: |
| 39 | + g.user = None |
| 40 | + else: |
| 41 | + g.user = ( |
| 42 | + get_db().execute("SELECT * FROM user WHERE id = ?", (user_id,)).fetchone() |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +@bp.route("/register", methods=("GET", "POST")) |
| 47 | +def register(): |
| 48 | + """Register a new user. |
| 49 | +
|
| 50 | + Validates that the username is not already taken. Hashes the |
| 51 | + password for security. |
| 52 | + """ |
| 53 | + if request.method == "POST": |
| 54 | + username = request.form["username"] |
| 55 | + password = request.form["password"] |
| 56 | + db = get_db() |
| 57 | + error = None |
| 58 | + |
| 59 | + if not username: |
| 60 | + error = "Username is required." |
| 61 | + elif not password: |
| 62 | + error = "Password is required." |
| 63 | + |
| 64 | + if error is None: |
| 65 | + try: |
| 66 | + db.execute( |
| 67 | + "INSERT INTO user (username, password) VALUES (?, ?)", |
| 68 | + (username, generate_password_hash(password)), |
| 69 | + ) |
| 70 | + db.commit() |
| 71 | + except db.IntegrityError: |
| 72 | + # The username was already taken, which caused the |
| 73 | + # commit to fail. Show a validation error. |
| 74 | + error = f"User {username} is already registered." |
| 75 | + else: |
| 76 | + # Success, go to the login page. |
| 77 | + return redirect(url_for("auth.login")) |
| 78 | + |
| 79 | + flash(error) |
| 80 | + |
| 81 | + return render_template("auth/register.html") |
| 82 | + |
| 83 | + |
| 84 | +@bp.route("/login", methods=("GET", "POST")) |
| 85 | +def login(): |
| 86 | + """Log in a registered user by adding the user id to the session.""" |
| 87 | + if request.method == "POST": |
| 88 | + username = request.form["username"] |
| 89 | + password = request.form["password"] |
| 90 | + db = get_db() |
| 91 | + error = None |
| 92 | + user = db.execute( |
| 93 | + "SELECT * FROM user WHERE username = ?", (username,) |
| 94 | + ).fetchone() |
| 95 | + |
| 96 | + if user is None: |
| 97 | + error = "Incorrect username." |
| 98 | + elif not check_password_hash(user["password"], password): |
| 99 | + error = "Incorrect password." |
| 100 | + |
| 101 | + if error is None: |
| 102 | + # store the user id in a new session and return to the index |
| 103 | + session.clear() |
| 104 | + session["user_id"] = user["id"] |
| 105 | + return redirect(url_for("index")) |
| 106 | + |
| 107 | + flash(error) |
| 108 | + |
| 109 | + return render_template("auth/login.html") |
| 110 | + |
| 111 | + |
| 112 | +@bp.route("/logout") |
| 113 | +def logout(): |
| 114 | + """Clear the current session, including the stored user id.""" |
| 115 | + session.clear() |
| 116 | + return redirect(url_for("index")) |
0 commit comments