0

I have finally got my entire program working for PSET8 Finance. I receive no errors in my dev console and all functions are working correctly after registering 15 users.

For some reason, though I get these error codes when I run check50:

:( registering user succeeds
   application raised an exception (rerun with --log for more details)
   sending POST request to /register
   exception raised in application: RuntimeError: UNIQUE constraint failed: 
   users.username

:( registration rejects duplicate username
    application raised an exception (rerun with --log for more details)
    sending POST request to /register
    checking that status code 200 is returned...
    sending POST request to /register
    exception raised in application: RuntimeError: UNIQUE constraint failed: 
    users.username

However when I try to register duplicate users everything runs correctly.

What could I be doing wrong here? Here is my /register function in application.py

@app.route("/register", methods=["GET", "POST"])
def register():
    if request.method == "POST":
        if not request.form.get("username"):
            return apology("Username Missing.")

        elif not request.form.get("password"):
           return apology("Password Missing.")

        elif request.form.get("password") != request.form.get("confirmation"):
           return apology("Sorry, password does not match. Please try Again!")

        hashpass = generate_password_hash(request.form.get("password"))

        added = db.execute(
           "INSERT INTO users(username,hash)VALUES(:username, :hash)",
           username=request.form.get("username"),hash=hashpass
        )

        rows = db.execute("SELECT * FROM users WHERE username = :username",
        username = request.form.get("username")
        )

        session["user_id"]=rows[0]["id"]

        return redirect("/")

    else:
        return render_template("register.html")
3
  • check50 calls the routes without going through the UI.Therefore the check function is not executed, therefore the INSERT in the register function will fail if it tries to insert a dupe. Therefore dupes need to be detected and prevented in the register function too. Jan 27 '20 at 14:33
  • Thank you, I did not realize the backend needed to check for a duplicate as well. But this makes sense for security purposes. Otherwise one could simply disable the Javascript to avoid the lockout.
    – KullideDev
    Jan 28 '20 at 12:10
  • I am now receiving status code 400 upon registering users, using /check to see if the username is available, and rejecting dupes via /register, despite my code checking on the backend for duplicated usernames. Will post a new question.
    – KullideDev
    Jan 28 '20 at 12:59

You must log in to answer this question.

Browse other questions tagged .