Login using database

This commit is contained in:
Emily 2019-04-26 03:24:19 +01:00
parent 885531b74b
commit 481d5650dc
4 changed files with 38 additions and 6 deletions

View File

@ -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"
}
}

View File

@ -9,7 +9,7 @@ class LoginForm(Form):
password = PasswordField("Password", [
validators.DataRequired(),
validators.Length(min=6)
validators.Length(min=4)
])
class User(UserMixin):

View File

@ -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
@ -16,3 +18,12 @@ if not os.path.isfile("config.json"):
with open("config.json", "r") as f:
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

View File

@ -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)