MCPcopy
hub / github.com/pallets/flask / register

Function register

examples/tutorial/flaskr/auth.py:47–81  ·  view source on GitHub ↗

Register a new user. Validates that the username is not already taken. Hashes the password for security.

()

Source from the content-addressed store, hash-verified

45
46@bp.route("/register", methods=("GET", "POST"))
47def 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"))

Callers

nothing calls this directly

Calls 5

redirectFunction · 0.90
url_forFunction · 0.90
flashFunction · 0.90
render_templateFunction · 0.90
get_dbFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…