Use flashing for sending errors to the front end

This commit is contained in:
Luke Else 2024-01-05 14:25:34 +00:00
parent 08479c1134
commit 968c19e3cf
3 changed files with 39 additions and 28 deletions

View File

@ -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,

View File

@ -5,16 +5,20 @@
<input type="password" id="password" name="password" placeholder="Password" required>
<input type="submit" id="login" value="Login">
</form>
{% if error != None %}
{% with messages = get_flashed_messages()%}
{% if messages %}
{% for message in messages %}
<label>
<input type="checkbox" class="alertCheckbox" autocomplete="off" />
<div class="alert error">
<span class="alertClose">X</span>
<span class="alertText">{{error}}
<span class="alertText">{{message}}
<br class="clear"/></span>
</div>
</label>
{% endfor%}
{% endif %}
{% endwith %}
<div id="create-account-wrap">
<p>Not a member? <a href="signup">Create Account</a><p>
</div>

View File

@ -8,16 +8,20 @@
<input type="password" id="password" name="password" minlength=8 placeholder="Password" required>
<input type="submit" id="Sign Up" value="Sign Up">
</form>
{% if error != None %}
{% with messages = get_flashed_messages()%}
{% if messages %}
{% for message in messages %}
<label>
<input type="checkbox" class="alertCheckbox" autocomplete="off" />
<div class="alert error">
<span class="alertClose">X</span>
<span class="alertText">{{error}}
<span class="alertText">{{message}}
<br class="clear"/></span>
</div>
</label>
{% endfor%}
{% endif %}
{% endwith %}
<div id="create-account-wrap">
<p>Already have an account? <a href="login">Login</a><p>
</div>