Created login functinality

This commit is contained in:
2024-01-02 22:22:14 +00:00
parent b26bd1a228
commit e0b04d13f6
9 changed files with 89 additions and 79 deletions

View File

@ -1,5 +1,5 @@
from . import blueprint
from flask import render_template, redirect, request
from flask import render_template, redirect, request, session
from controllers.database.user import UserController
from models.users.customer import Customer
from hashlib import sha512
@ -8,7 +8,7 @@ from hashlib import sha512
# Function responsible for displaying the main landing page of the site
@blueprint.route('/')
def welcome_page():
return render_template('index.html', content="content.html")
return render_template('index.html', content="content.html", user = session.get('user'))
@ -16,12 +16,23 @@ def welcome_page():
# Function responsible for delivering the Login page for the site
@blueprint.route('/login')
def display_login():
return render_template('index.html', content="login.html")
return render_template('index.html', content="login.html", user = session.get('user'))
# Function responsible for handling logins to the site
@blueprint.post('/login')
def login():
print("Tryin to login as " + request.form['username'])
database = UserController()
user = database.read(request.form['username'])
# No user found
if user == None:
return redirect("/login")
# Incorrect Password
if sha512(request.form['password'].encode()).hexdigest() != user.password:
return redirect("/login")
session['user'] = user.username
return redirect("/")
@ -29,7 +40,7 @@ def login():
# Function responsible for delivering the Signup page for the site
@blueprint.route('/signup')
def display_signup():
return render_template('index.html', content="signup.html")
return render_template('index.html', content="signup.html", user = session.get('user'))
# Function responsible for handling signups to the site
@blueprint.post('/signup')
@ -38,12 +49,20 @@ def signup():
database.create(Customer(
0,
request.form['username'],
request.form['email'],
sha512(request.form['password'].encode()).hexdigest(), # Hashed as soon as it is recieved on the backend
request.form['firstname'],
request.form['lastname'],
request.form['email'],
"123",
sha512(request.form['password'].encode()).hexdigest(), # Hashed as soon as it is recieved on the backend
"Customer"
))
return redirect("/")
# Code 307 Preserves the original request (POST)
return redirect("/login", code=307)
# Function responsible for handling logouts from the site
@blueprint.route('/logout')
def logout():
session.pop('user')
return redirect("/")