diff --git a/default_config.json b/default_config.json index aa69d5d..614eac6 100644 --- a/default_config.json +++ b/default_config.json @@ -11,5 +11,12 @@ "auto_pull_and_restart": false, "webhook_endpoint": "/api/git_commit", "secret": "iOnlyHavePullAccess" + }, + "mysql": { + "host": "localhost", + "port": 3306, + "user": "husstanden", + "passwd": "", + "db": "db_husstanden" } } \ No newline at end of file diff --git a/forms/login.py b/forms/login.py index bf9bd0a..a024c21 100644 --- a/forms/login.py +++ b/forms/login.py @@ -9,7 +9,7 @@ class LoginForm(Form): password = PasswordField("Password", [ validators.DataRequired(), - validators.Length(min=6) + validators.Length(min=4) ]) class User(UserMixin): diff --git a/objects/glob.py b/objects/glob.py index 08a05e6..da3921a 100644 --- a/objects/glob.py +++ b/objects/glob.py @@ -1,11 +1,13 @@ import os import json import shutil +import mysql.connector # ------------------------------------------------------------------------------ # Global variables that is None by default and gets overwritten in other modules app = None # main.py -> Flask App +sql_conn = None # ------------------------------------------------------------------------------ # Global variables that initializes on first load of module @@ -15,4 +17,13 @@ if not os.path.isfile("config.json"): shutil.copy("default_config.json", "config.json") with open("config.json", "r") as f: - config = json.load(f) \ No newline at end of file + config = json.load(f) + +def make_sql_connection(): + return mysql.connector.connect(**config["mysql"]) + +def get_sql_connection(): + global sql_conn + if sql_conn is None or not sql_conn.is_connected(): + sql_conn = make_sql_connection() + return sql_conn diff --git a/routes.py b/routes.py index 4090e60..01b1087 100644 --- a/routes.py +++ b/routes.py @@ -27,13 +27,27 @@ def login(): form = LoginForm(request.form) if request.method == "POST" and form.validate(): - # TODO: get uuid from database - uuid = 0 - if uuid < 0: + + 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") return render_template("login.html", form=form) - user = User(uuid) + user = User(int(uuid[0])) flask_login.login_user(user) logged_in_users.append(user)