Skip to content

Commit

Permalink
login and registration are not fully functional
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan-Sell committed Nov 13, 2024
1 parent 1181ad4 commit 2b80402
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
46 changes: 26 additions & 20 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from src.forms.user_forms import RegisterForm, LogInForm
from src.models import Base, SessionLocal, Users, engine
from src.repository.users_repository import UsersRepository
from src.security import generate_password_hash

app = Flask(__name__, static_folder="../static", template_folder="../templates")
Expand All @@ -20,7 +21,11 @@

@login_manager.user_loader
def load_user(user_id):
return Users.query.get(int(user_id))
session = SessionLocal()
user_repo = UsersRepository(session)
user = user_repo.find_user_by_id(user_id)
session.close()
return user


@app.route("/")
Expand All @@ -32,14 +37,20 @@ def login():
username = form.username.data
password = form.password.data

user = Users.query.filter_by(username=username).first()
print("Form submitted with:", username, password)
session = SessionLocal()
user_repo = UsersRepository(session)
user = user_repo.find_user_by_username(username)

if user is None:
session.close()
flash("That email does not exist. Please try again.", "danger")
elif not user.check_password(password, user.password_hash):
elif not user_repo.check_password(password, user.password_hash):
session.close()
flash("Invalid password. Please try again.", "danger")
else:
login_user(user)
session.close()
return redirect(url_for("dashboard"))

return render_template("login.html", form=form)
Expand All @@ -60,24 +71,19 @@ def register():

# create a database session
session = SessionLocal()
try:
# check if user already exists
if session.query(Users).filter_by(username=username).first() is not None:
flash(
"Username already exists. Please choose a different one.", "danger"
)
return redirect(url_for("register"))

hashed_password = generate_password_hash(password)
new_user = Users(username=username, password_hash=hashed_password)
session.add(new_user)
session.commit()

flash("Account successfully created. You can now log in.", "success")
return redirect(url_for("login"))

finally:
user_repo = UsersRepository(session)

if user_repo.find_user_by_username(username) is not None:
flash(
"Username already exists. Please choose a different one.", "danger"
)
session.close()
return redirect(url_for("register"))

user_repo.add_user(username, password)
session.close()
flash("Account successfully created. You can now log in.", "success")
return redirect(url_for("login"))

return render_template("register.html", form=form)

Expand Down
2 changes: 0 additions & 2 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
<div class="collapse navbar-collapse">
<ul class="navbar-nav ml-auto">
{% if current_user.is_authenticated %}
<!-- <li class="nav-item"><a class="nav-link" href="{{ url_for('tasks') }}">My Tasks</a></li>
<li class="nav-item"><a class="nav-link" href="{{ url_for('logout') }}">Logout</a></li> -->
{% else %}
<li class="nav-item"><a class="nav-link" href="{{ url_for('login') }}">Login</a></li>
{% endif %}
Expand Down
5 changes: 3 additions & 2 deletions templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
<div class="card p-4" style="width: 400px; border-radius: 15px; box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.1);">
<div class="text-center mb-4">
<h2 class="mt-2" style="font-family: 'Brush Script MT', cursive; font-size: 2.5rem; color: #333;">ToDo List App</h2>
<img src="{{ url_for('static', filename='img/to_do_list.png') }}" alt="ToDo List Icon" width="60">
<img src="{{ url_for('static', filename='img/to_do_list.png') }}" alt="ToDo List Icon" width="70">
</div>
<form method="POST" action="{{ url_for('login') }}">
{{ form.hidden_tag() }} <!-- CSRF token -->
<div class="form-group">
<label for="username">Email</label>
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username" required>
</div>
<div class="form-group mt-3">
Expand Down
2 changes: 1 addition & 1 deletion templates/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<div class="card p-4" style="width: 400px; border-radius: 15px; box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.1);">
<div class="text-center mb-4">
<h2 class="mt-2" style="font-family: 'Brush Script MT', cursive; font-size: 2.5rem; color: #333;">ToDo List App</h2>
<img src="{{ url_for('static', filename='img/to_do_list.png') }}" alt="ToDo List Icon" width="60">
<img src="{{ url_for('static', filename='img/to_do_list.png') }}" alt="ToDo List Icon" width="70">
</div>
<form method="POST" action="{{ url_for('register') }}">
{{ form.hidden_tag() }}
Expand Down

0 comments on commit 2b80402

Please sign in to comment.