0

I'm getting the following error from check50 for Pset7 Finance:

enter image description here

Why does check50 fail to login as a registered user?! It works perfectly fine for me to log in.

That login function is contained in the CS50 distribution. I doubt that there's anything wrong with the code. And I didn't touch it.

Also, there doesn't seem to be anything wrong according to flask when I log in myself:

enter image description here

Here's the code just in case:

@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 = :username", 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")

login.html:

{% extends "layout.html" %}

{% block title %}
    Log In
{% endblock %}

{% block main %}
    <form action="/login" method="post">
        <div class="form-group">
            <input autocomplete="off" autofocus class="form-control" name="username" placeholder="Username" type="text"/>
        </div>
        <div class="form-group">
            <input class="form-control" name="password" placeholder="Password" type="password"/>
        </div>
        <button class="btn btn-primary" type="submit">Log In</button>
    </form>
{% endblock %}

Thank you for your help! :)

2
  • Have you checked if your POST field names are username and password? Commented Jan 6, 2018 at 6:03
  • Do you mean in the html-file? Yes, please see added code above.
    – Faye_108
    Commented Jan 6, 2018 at 8:50

1 Answer 1

1

Found the mistake. The reason why I could login and check50 couldn't was this code line in the register function:

    hash = generate_password_hash(request.form.get("username"))

I was always using the same value for username and password. That's why the line above worked for me.

correct code must be:

    hash = generate_password_hash(request.form.get("password"))
1
  • Did you write generate_passoword_hash or it was an inbuilt function Commented Apr 29, 2020 at 5:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .