Skip to content

Commit

Permalink
added the ability to log in
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmaclaren committed Apr 2, 2024
1 parent 498af37 commit 2d56606
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
34 changes: 31 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,40 @@ def after_request(response):
return response

@app.route("/")
def home():
def index():
return render_template("index.html")

@app.route("/login")
@app.route("/login", methods=["GET", "POST"])
def login():
return render_template("login.html")

session.clear()

if request.method == "POST":

if not request.form.get("username"):
return apology("must provide username", 400)

elif not request.form.get("password"):
return apology("must provide password", 400)

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

data = rows.fetchall()

if len(data) != 1 or not check_password_hash(
data[0][2], request.form.get("password")
):
return apology("Invalid username and/or password", 400)

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

return redirect("/")

elif request.method == "GET":
return render_template("login.html")

@app.route("/register", methods=["GET", "POST"])
def register():
Expand Down
8 changes: 4 additions & 4 deletions templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
{% block main %}
<form action="/login" method="post">
<div class="py-1 text-center">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control mx-auto w-auto" id="username" aria-describedby="emailHelp">
<label for="username">Username</label>
<input type="text" class="form-control mx-auto w-auto" name="username" aria-describedby="emailHelp">
</div>
<div class="py-1 text-center">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control mx-auto w-auto" id="password">
<label for="password">Password</label>
<input type="password" class="form-control mx-auto w-auto" name="password">
</div>
<div class="py-3 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
Expand Down

0 comments on commit 2d56606

Please sign in to comment.