Use flashing for sending errors to the front end
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
from . import blueprint
|
||||
from flask import render_template, redirect, request, session
|
||||
from flask import render_template, redirect, request, session, flash
|
||||
from controllers.database.user import UserController
|
||||
from models.users.customer import Customer
|
||||
from hashlib import sha512
|
||||
@ -15,8 +15,8 @@ def welcome_page():
|
||||
### LOGIN FUNCTIONALITY
|
||||
# Function responsible for delivering the Login page for the site
|
||||
@blueprint.route('/login')
|
||||
def display_login(error: str = None):
|
||||
return render_template('index.html', content="login.html", user = session.get('user'), error = error)
|
||||
def display_login():
|
||||
return render_template('index.html', content="login.html", user = session.get('user'))
|
||||
|
||||
# Function responsible for handling logins to the site
|
||||
@blueprint.post('/login')
|
||||
@ -28,12 +28,14 @@ def login():
|
||||
# No user found
|
||||
if user == None:
|
||||
error = "No user found with the username " + request.form['username']
|
||||
return display_login(error)
|
||||
flash(error)
|
||||
return redirect("/login")
|
||||
|
||||
# Incorrect Password
|
||||
if sha512(request.form['password'].encode()).hexdigest() != user.password:
|
||||
error = "Incorrect Password"
|
||||
return display_login(error)
|
||||
flash(error)
|
||||
return redirect("/login")
|
||||
|
||||
session['user'] = user.username
|
||||
return redirect("/")
|
||||
@ -42,8 +44,8 @@ def login():
|
||||
### SIGNUP FUNCTIONALITY
|
||||
# Function responsible for delivering the Signup page for the site
|
||||
@blueprint.route('/signup')
|
||||
def display_signup(error: str = None):
|
||||
return render_template('index.html', content="signup.html", user = session.get('user'), error = error)
|
||||
def display_signup():
|
||||
return render_template('index.html', content="signup.html", user = session.get('user'))
|
||||
|
||||
# Function responsible for handling signups to the site
|
||||
@blueprint.post('/signup')
|
||||
@ -53,7 +55,8 @@ def signup():
|
||||
# User already exists
|
||||
if database.read(request.form['username']) != None:
|
||||
error = "User, " + request.form['username'] + " already exists"
|
||||
return display_signup(error)
|
||||
flash(error)
|
||||
return redirect("/signup")
|
||||
|
||||
database.create(Customer(
|
||||
0,
|
||||
|
Reference in New Issue
Block a user