Skip to content

Commit

Permalink
added session and logout abilities
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmaclaren committed Apr 3, 2024
1 parent 2d56606 commit 9ab0faf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
18 changes: 13 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from werkzeug.security import check_password_hash, generate_password_hash

app = Flask(__name__)

app.secret_key = "test"
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
Expand All @@ -30,6 +30,8 @@ def after_request(response):

@app.route("/")
def index():
#user_id = session["user_id"]

return render_template("index.html")

@app.route("/login", methods=["GET", "POST"])
Expand Down Expand Up @@ -57,13 +59,19 @@ def login():
):
return apology("Invalid username and/or password", 400)

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

return redirect("/")

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

@app.route("/logout")
def logout():
session.clear()
#session.pop("user_id", None)
return render_template("login.html")

@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user"""
Expand All @@ -78,11 +86,9 @@ def register():
elif request.form.get("password") != request.form.get("confirmation"):
return apology("passwords must match", 400)

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

data = rows.fetchall()

Expand All @@ -100,8 +106,10 @@ def register():
)
conn.commit()

new_user_id = new_user.lastrowid


"""session["user_id"] = new_user"""
session["user_id"] = new_user_id

return redirect("/")

Expand Down
48 changes: 22 additions & 26 deletions templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,32 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
<title>{% block content %}{% endblock %}</title>
<title>CURL {% block content %}{% endblock %}</title>
</head>
<body>
<header>
<nav class="navbar navbar-expand-lg navbar-light" style="background-color: #9dbeb7">
<a class="navbar-brand" href="#">Curl</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<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>
<li class="nav-item">
<a class="nav-link" href="/training">Training</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/aboutme">About Me</a>
</li>
</ul>
<div class="container-fluid">
<a class="navbar-brand" href="#">Curl</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
{% if session["user_id"] %}
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link" href="/">Home</a></li>
<li class="nav-item"><a class="nav-link" href="/history">History</a></li>
<li class="nav-item"><a class="nav-link" href="/training">Training</a></li>
<li class="nav-item"><a class="nav-link" href="/aboutme">About Me</a>
<li class="nav-item"><a class="nav-link" href="/logout">Logout</a></li>
</ul>
{% else %}
<ul class="navbar-nav">
<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>
</ul>
{% endif %}
</div>
</div>
</nav>
</header>
Expand Down

0 comments on commit 9ab0faf

Please sign in to comment.