Use flashing for sending errors to the front end
This commit is contained in:
parent
08479c1134
commit
968c19e3cf
@ -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,
|
||||||
|
@ -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()%}
|
||||||
|
{% if messages %}
|
||||||
|
{% for message in messages %}
|
||||||
<label>
|
<label>
|
||||||
<input type="checkbox" class="alertCheckbox" autocomplete="off" />
|
<input type="checkbox" class="alertCheckbox" autocomplete="off" />
|
||||||
<div class="alert error">
|
<div class="alert error">
|
||||||
<span class="alertClose">X</span>
|
<span class="alertClose">X</span>
|
||||||
<span class="alertText">{{error}}
|
<span class="alertText">{{message}}
|
||||||
<br class="clear"/></span>
|
<br class="clear"/></span>
|
||||||
</div>
|
</div>
|
||||||
</label>
|
</label>
|
||||||
|
{% endfor%}
|
||||||
{% endif %}
|
{% 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>
|
||||||
|
@ -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()%}
|
||||||
|
{% if messages %}
|
||||||
|
{% for message in messages %}
|
||||||
<label>
|
<label>
|
||||||
<input type="checkbox" class="alertCheckbox" autocomplete="off" />
|
<input type="checkbox" class="alertCheckbox" autocomplete="off" />
|
||||||
<div class="alert error">
|
<div class="alert error">
|
||||||
<span class="alertClose">X</span>
|
<span class="alertClose">X</span>
|
||||||
<span class="alertText">{{error}}
|
<span class="alertText">{{message}}
|
||||||
<br class="clear"/></span>
|
<br class="clear"/></span>
|
||||||
</div>
|
</div>
|
||||||
</label>
|
</label>
|
||||||
|
{% endfor%}
|
||||||
{% endif %}
|
{% 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>
|
||||||
|
Loading…
Reference in New Issue
Block a user