Register a new user. Validates that the username is not already taken. Hashes the password for security.
()
| 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")) |
nothing calls this directly
no test coverage detected
searching dependent graphs…