0

Have a problem with check50. I'm able to register, log in and all but check50 keeps denying it (the login part):


:) application.py exists
    checking that application.py exists...
:) application starts up
    sending GET request to /
    checking that status code 200 is returned...
:) register page has all required elements
    sending GET request to /register
    found required "username" field
    found required "password" field
    found required "confirmation" field
:) registering user succeeds
    sending POST request to /register
    checking that status code 200 is returned...
:) registration with an empty field fails
    sending POST request to /register
    checking that status code 400 is returned...
    sending POST request to /register
    checking that status code 400 is returned...
    sending POST request to /register
    checking that status code 400 is returned...
:) registration with password mismatch fails
    sending POST request to /register
    checking that status code 400 is returned...
:) registration rejects duplicate username
    sending POST request to /register
    checking that status code 200 is returned...
    sending POST request to /register
    checking that status code 400 is returned...
:) login page has all required elements
    sending GET request to /login
    found required "username" field
    found required "password" field
:( logging in as registered user succceeds
    application raised an exception (see the log for more details)
    sending POST request to /login
    exception raised in application: TypeError: 'NoneType' object is not subscriptable

Not sure where the problem is.

This is my register function:

@app.route("/register", methods=["GET", "POST"])
def register():
    """Register user"""

    if request.method == "POST":

        # Ensure username was submitted: OK
        if not request.form.get("username"):
            return apology("must provide username", 400)

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

        # Ensure username exists: OK
        if len(rows) > 0:
            return apology("username already exist", 400)

        # Ensure password was submitted: OK
        elif not request.form.get("password"):
            return apology("must provide password", 400)

        # Ensure confirmation is submitted: OK
        elif not request.form.get("confirmation"):
            return apology("must confirm password", 400)

        # Check password mach confirmation:
        elif request.form.get("password") != request.form.get("confirmation"):
            return apology("Password dont mach", 400)

        #hash password:
        hash = generate_password_hash(request.form.get("password")) #, method='pbkdf2:sha256', salt_length=8)


        # Remember registrant
        db.execute("INSERT INTO users (username, hash) VALUES(?, ?)", request.form.get("username"), hash)

    else:
        return render_template("register.html")
    return apology("Great, you passed", 200)

and the log in:

@app.route("/login", methods=["GET", "POST"])
def login():
    """Log user in"""

    # Forget any user_id
    session.clear()

    # User reached route via POST (as by submitting a form via POST)
    if request.method == "POST":

        # Ensure username was submitted
        if not request.form.get("username"):
            return apology("must provide username", 403)

        # Ensure password was submitted
        elif not request.form.get("password"):
            return apology("must provide password", 403)

        # Query database for username
        rows = db.execute("SELECT * FROM users WHERE username = ?", request.form.get("username"))

        # Ensure username exists and password is correct
        if len(rows) != 1 or not check_password_hash(rows[0]["hash"], request.form.get("password")):
            return apology("invalid username and/or password", 403)

        # Remember which user has logged in
        session["user_id"] = rows[0]["id"]

        # Redirect user to home page
        return redirect("/")

    # User reached route via GET (as by clicking a link or via redirect)
    else:
        return render_template("login.html")

First post, sorry if i messed up something.

2
  • As long as the login function is not modified, suspect the problem is in the index function, since that is where app proceeds after login. Oct 27 at 10:33
  • @DinoCoderSaurus Thanks a lot, i had a stupid mistake in index
    – steeplight
    Oct 27 at 13:31

You must log in to answer this question.

Browse other questions tagged .