I've tried everything that I thought could be the problem but nothing is working. I registered and tried logging in but I keep getting '403 Invalid username and/or password' error This is my login function in application.py
@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")
This is my register function:
@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user"""
if request.method == "POST":
username = request.form.get("username")
password = request.form.get("password")
confirmation = request.form.get("confirmation")
rows = db.execute("SELECT * FROM users WHERE username = ?", username)
# making sure a username is provided and it isn't already in use by someone else
if not username:
return apology("Please enter a username", 400)
elif len(rows) != 0:
return apology("Username taken", 400)
# making sure password is input
elif not password:
return apology("Please enter your password", 400)
# Require users’ password to have at least 8 characters, letters and numbers
if len(password) < 8:
return apology("Make sure your password is at least 8 letters", 400)
elif re.search('[0-9]', password) is None:
return apology("Make sure your password has a number in it", 400)
elif re.search('[A-Z]', password) is None:
return apology("Make sure your password has a capital letter in it", 400)
# making sure that the password and confirmation password match
elif not password == confirmation:
return apology("Passwords do not match", 400)
else:
# generating the password hash
hash = generate_password_hash("password")
# add the user to the database and store their userid in a session
user_id = db.execute("INSERT INTO users (username, hash) VALUES (?, ?)", username, hash)
# session["user_id"] = user_id
return redirect("/")
else:
return render_template("register.html")
My register.html:
{% extends "layout.html" %}
{% block title %}
Log In
{% endblock %}
{% block main %}
<form action="/register" method="post">
<div>
<input autocomplete="off" autofocus class="form-control" name="username" placeholder="Username" type="text">
</div>
<div>
<input class="form-control" name="password" placeholder="Password" type="password">
</div>
<div>
<input class="form-control" name="confirmation" placeholder="Confirm password" type="password">
</div>
<button type="submit">Log In</button>
</form>
{% endblock %}
And my 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 %}
Please help. I've been staring at this thing for too long.