-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored codebase to create auth_controller.py. All control functio…
…nality has been removed from view, i.e., main.py
- Loading branch information
1 parent
857d2e9
commit d51add9
Showing
8 changed files
with
96 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from flask import Blueprint, flash, redirect, render_template, url_for | ||
from flask_login import login_required, login_user, logout_user | ||
|
||
from src.forms.user_forms import LogInForm, RegisterForm | ||
from src.models import SessionLocal | ||
from src.repository.users_repository import UsersRepository | ||
|
||
auth_blueprint = Blueprint("auth", __name__) | ||
|
||
|
||
@auth_blueprint.route("/") | ||
@auth_blueprint.route("/login", methods=["GET", "POST"]) | ||
def login(): | ||
form = LogInForm() | ||
|
||
if form.validate_on_submit(): | ||
username = form.username.data | ||
password = form.password.data | ||
|
||
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_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("tasks.view_tasks", user_id=user.id)) | ||
|
||
return render_template("login.html", form=form) | ||
|
||
|
||
@auth_blueprint.route("/logout") | ||
@login_required | ||
def logout(): | ||
logout_user() | ||
flash("You have successfully logged out.", "info") | ||
return redirect(url_for("auth.login")) | ||
|
||
|
||
@auth_blueprint.route("/register", methods=["GET", "POST"]) | ||
def register(): | ||
form = RegisterForm() | ||
if form.validate_on_submit(): | ||
username = form.username.data | ||
password = form.password.data | ||
|
||
# create a database session | ||
session = SessionLocal() | ||
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("auth.register")) | ||
|
||
user_repo.add_user(username, password) | ||
session.close() | ||
flash("Account successfully created. You can now log in.", "success") | ||
return redirect(url_for("auth.login")) | ||
|
||
return render_template("register.html", form=form) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters