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 . 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 controllers.database.user import UserController
from models.users.customer import Customer from models.users.customer import Customer
from hashlib import sha512 from hashlib import sha512
@ -15,8 +15,8 @@ def welcome_page():
### LOGIN FUNCTIONALITY ### LOGIN FUNCTIONALITY
# Function responsible for delivering the Login page for the site # Function responsible for delivering the Login page for the site
@blueprint.route('/login') @blueprint.route('/login')
def display_login(error: str = None): def display_login():
return render_template('index.html', content="login.html", user = session.get('user'), error = error) return render_template('index.html', content="login.html", user = session.get('user'))
# Function responsible for handling logins to the site # Function responsible for handling logins to the site
@blueprint.post('/login') @blueprint.post('/login')
@ -28,12 +28,14 @@ def login():
# No user found # No user found
if user == None: if user == None:
error = "No user found with the username " + request.form['username'] error = "No user found with the username " + request.form['username']
return display_login(error) flash(error)
return redirect("/login")
# Incorrect Password # Incorrect Password
if sha512(request.form['password'].encode()).hexdigest() != user.password: if sha512(request.form['password'].encode()).hexdigest() != user.password:
error = "Incorrect Password" error = "Incorrect Password"
return display_login(error) flash(error)
return redirect("/login")
session['user'] = user.username session['user'] = user.username
return redirect("/") return redirect("/")
@ -42,8 +44,8 @@ def login():
### SIGNUP FUNCTIONALITY ### SIGNUP FUNCTIONALITY
# Function responsible for delivering the Signup page for the site # Function responsible for delivering the Signup page for the site
@blueprint.route('/signup') @blueprint.route('/signup')
def display_signup(error: str = None): def display_signup():
return render_template('index.html', content="signup.html", user = session.get('user'), error = error) return render_template('index.html', content="signup.html", user = session.get('user'))
# Function responsible for handling signups to the site # Function responsible for handling signups to the site
@blueprint.post('/signup') @blueprint.post('/signup')
@ -53,7 +55,8 @@ def signup():
# User already exists # User already exists
if database.read(request.form['username']) != None: if database.read(request.form['username']) != None:
error = "User, " + request.form['username'] + " already exists" error = "User, " + request.form['username'] + " already exists"
return display_signup(error) flash(error)
return redirect("/signup")
database.create(Customer( database.create(Customer(
0, 0,

View File

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

View File

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