Login fetch data
This commit is contained in:
parent
481d5650dc
commit
538ce926f8
|
@ -1,6 +1,8 @@
|
|||
from wtforms import Form, BooleanField, StringField, PasswordField, validators
|
||||
from flask_login import UserMixin
|
||||
|
||||
from objects import glob
|
||||
|
||||
class LoginForm(Form):
|
||||
email = StringField("Email", [
|
||||
validators.DataRequired(),
|
||||
|
@ -13,12 +15,31 @@ class LoginForm(Form):
|
|||
])
|
||||
|
||||
class User(UserMixin):
|
||||
def __init__(self, uuid):
|
||||
self.id = uuid
|
||||
id = -1
|
||||
email = ""
|
||||
password = ""
|
||||
firstname = ""
|
||||
surname = ""
|
||||
def __init__(self, login):
|
||||
self.fetch_from_db(login)
|
||||
|
||||
"""
|
||||
self.name = name
|
||||
self.email = email
|
||||
self.password = password
|
||||
"""
|
||||
def fetch_from_db(self, login):
|
||||
conn = glob.get_sql_connection()
|
||||
cur = conn.cursor()
|
||||
|
||||
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
|
||||
|
|
28
routes.py
28
routes.py
|
@ -27,28 +27,12 @@ def login():
|
|||
|
||||
form = LoginForm(request.form)
|
||||
if request.method == "POST" and form.validate():
|
||||
|
||||
conn = glob.get_sql_connection()
|
||||
cur = conn.cursor()
|
||||
|
||||
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")
|
||||
try:
|
||||
user = User((form.email.data, form.password.data))
|
||||
except Exception as e:
|
||||
flash(str(e), "danger")
|
||||
return render_template("login.html", form=form)
|
||||
|
||||
user = User(int(uuid[0]))
|
||||
|
||||
flask_login.login_user(user)
|
||||
logged_in_users.append(user)
|
||||
|
||||
|
@ -70,7 +54,9 @@ def unauthorized_handler_err():
|
|||
|
||||
@login_manager.user_loader
|
||||
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
|
||||
def unauthorized_handler():
|
||||
|
|
|
@ -56,7 +56,7 @@
|
|||
</div>
|
||||
<div class="col">
|
||||
<div class="rndblock"> <!-- OR INCLUDE BACKGROUND TEXTURE -->
|
||||
T <!-- FIRST CHAR OF USER -->
|
||||
{{ current_user.firstname[0]|upper }} <!-- FIRST CHAR OF USER -->
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue
Block a user