Login fetch data

This commit is contained in:
Emily 2019-04-26 03:52:54 +01:00
parent 481d5650dc
commit 538ce926f8
3 changed files with 36 additions and 29 deletions

View File

@ -1,6 +1,8 @@
from wtforms import Form, BooleanField, StringField, PasswordField, validators from wtforms import Form, BooleanField, StringField, PasswordField, validators
from flask_login import UserMixin from flask_login import UserMixin
from objects import glob
class LoginForm(Form): class LoginForm(Form):
email = StringField("Email", [ email = StringField("Email", [
validators.DataRequired(), validators.DataRequired(),
@ -13,12 +15,31 @@ class LoginForm(Form):
]) ])
class User(UserMixin): class User(UserMixin):
def __init__(self, uuid): id = -1
self.id = uuid email = ""
password = ""
firstname = ""
surname = ""
def __init__(self, login):
self.fetch_from_db(login)
""" def fetch_from_db(self, login):
self.name = name conn = glob.get_sql_connection()
self.email = email cur = conn.cursor()
self.password = password
"""
cur.execute("""
SELECT *
FROM Bruker
WHERE Epost = %s
AND Passord = %s
LIMIT 1;
""", login)
user = cur.fetchone()
cur.close()
if user is None:
raise Exception("Invalid login")
self.id, self.email, self.password, self.firstname, self.surname = user

View File

@ -27,28 +27,12 @@ def login():
form = LoginForm(request.form) form = LoginForm(request.form)
if request.method == "POST" and form.validate(): if request.method == "POST" and form.validate():
try:
conn = glob.get_sql_connection() user = User((form.email.data, form.password.data))
cur = conn.cursor() except Exception as e:
flash(str(e), "danger")
cur.execute("""
SELECT BrukerID
FROM Bruker
WHERE Epost = %s
AND Passord = %s
LIMIT 1;
""", (form.email.data, form.password.data))
uuid = cur.fetchone()
cur.close()
if uuid is None:
flash("Invalid login", "danger")
return render_template("login.html", form=form) return render_template("login.html", form=form)
user = User(int(uuid[0]))
flask_login.login_user(user) flask_login.login_user(user)
logged_in_users.append(user) logged_in_users.append(user)
@ -70,7 +54,9 @@ def unauthorized_handler_err():
@login_manager.user_loader @login_manager.user_loader
def load_user(uuid): def load_user(uuid):
return User(uuid) uuid = int(uuid)
lst = [x for x in logged_in_users if x.id == uuid]
return lst[0] if len(lst) > 0 else None
@login_manager.unauthorized_handler @login_manager.unauthorized_handler
def unauthorized_handler(): def unauthorized_handler():

View File

@ -56,7 +56,7 @@
</div> </div>
<div class="col"> <div class="col">
<div class="rndblock"> <!-- OR INCLUDE BACKGROUND TEXTURE --> <div class="rndblock"> <!-- OR INCLUDE BACKGROUND TEXTURE -->
T <!-- FIRST CHAR OF USER --> {{ current_user.firstname[0]|upper }} <!-- FIRST CHAR OF USER -->
</div> </div>
</div> </div>
</div> </div>