Skip to content

Commit

Permalink
started working on the log in and register options
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmaclaren committed Mar 13, 2024
1 parent 5dd522d commit 5f9f6cd
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 10 deletions.
47 changes: 45 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
from flask import Flask, render_template, request
from flask import Flask, render_template, request, session
from helpers import login_required
from werkzeug.security import check_password_hash, generate_password_hash
import sqlite3

db = sqlite3.connect('workout.db')


app = Flask(__name__)

Expand All @@ -13,17 +19,54 @@ def login():

@app.route("/register")
def register():
return render_template("register.html")
"""Register user"""
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)

elif request.form.get("password") != request.form.get("confirmation"):
return apology("passwords must match", 400)

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

if len(rows) >= 1:
return apology(
"username is already taken, please use another username", 400
)
else:
hash_password = generate_password_hash(request.form.get("password"))

new_user = db.execute(
"INSERT INTO users (username, hash) VALUES (?, ?)",
request.form.get("username"),
hash_password
)

session["user_id"] = new_user

return redirect("/")

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

@app.route("/aboutme")
@login_required
def aboutme():
return render_template("aboutme.html")

@app.route("/training")
@login_required
def training():
return render_template("training.html")

@app.route("/history")
@login_required
def history():
return render_template("history.html")

Expand Down
10 changes: 10 additions & 0 deletions helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from functools import wraps
from flask import request, redirect, url_for

def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get("user_id") is None:
return redirect("/login")
return f(*args, **kwargs)
return decorated_function
5 changes: 4 additions & 1 deletion templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="/index">Home</a>
<a class="nav-link" href="/">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/login">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/register">Register</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/history">History</a>
</li>
Expand Down
13 changes: 7 additions & 6 deletions templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@

{% block main %}
<form action="/login" method="post">
<div class="form-group">
<div class="py-1 text-center">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
<input type="email" class="form-control mx-auto w-auto" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
</div>
<div class="form-group">
<div class="py-1 text-center">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
<input type="password" class="form-control mx-auto w-auto" id="exampleInputPassword1" placeholder="Password">
</div>
<div class="py-3 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
{% endblock %}
16 changes: 15 additions & 1 deletion templates/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@

{% block main %}
<form action="/register" method="post">

<div class="py-1 text-center">
<label for="Username">Email address</label>
<input autocomplete="off" autofocus class="form-control mx-auto w-auto" name="username" placeholder="Enter email" type="text">
</div>
<div class="py-1 text-center">
<label for="Password">Password</label>
<input class="form-control mx-auto w-auto" name="password" placeholder="Enter password" type="password">
</div>
<div class="py-1 text-center">
<label for="Confirmation">Confirmation</label>
<input class="form-control mx-auto w-auto" name="confirmation" placeholder="Confirm Password" type="password">
</div>
<div class="py-3 text-center">
<button class="btn btn-primary" type="submit">Register</button>
</div>
</form>
{% endblock %}

0 comments on commit 5f9f6cd

Please sign in to comment.