diff --git a/backend/add_user.py b/backend/add_user.py index 4a66feb..515372b 100644 --- a/backend/add_user.py +++ b/backend/add_user.py @@ -2,21 +2,29 @@ import pymongo import getpass import uuid -myclient = pymongo.MongoClient("mongodb://admin:pass@localhost:27017/") +# initialize connection to database +config = { + "username": "admin", + "password": "pass", + "server": "localhost:27017", +} +connector = "mongodb://{}:{}@{}".format(config["username"],config["password"], config["server"]) +client = pymongo.MongoClient(connector) +db = client["groclist"] + username = input("Username: ") password = getpass.getpass("Password: ") uuid = str(uuid.uuid4()) email = input("Email-adres: ") full_name = input("Volledige naam: ") -mydb = myclient["groclist_db"] -mycol = mydb["groclist_users"] +col = db["cred"] -mydict = {"username": username, "password": password, "groclist_uuid": uuid, "email": email, "name": full_name} -if mycol.find_one({"username": username}): +mydict = {"username": username, "password": password, "uuid": uuid, "email": email, "name": full_name} +if col.find_one({"username": username}): print("Username already exists\n") exit() else: - x = mycol.insert_one(mydict) + x = col.insert_one(mydict) print("User created") diff --git a/frontend/groclist.py b/frontend/groclist.py index 20de421..dd8302d 100644 --- a/frontend/groclist.py +++ b/frontend/groclist.py @@ -8,26 +8,35 @@ config = { "password": "pass", "server": "localhost:27017", } -connector = "mongodb://{}:{}@{}".format(config["username"],config["password"], config["server"] +connector = "mongodb://{}:{}@{}".format(config["username"],config["password"], config["server"]) client = pymongo.MongoClient(connector) db = client["groclist"] @app.route("/") def main(): - return render_template("login.html") + return render_template("index.html") -@app.route("/login", methods=["POST"]) -def login() +@app.route("/login", methods=["GET","POST"]) +def login(): error = None - username = request.form["username"] - password = request.form["password"] - if db.cred.find_one({"username": username, "password": password}) - auth_user_id = db.cred.find_one({"username": username},{"_id", 1}) - return redirect(url_for("welcome", id=auth_user_id["_id"])) - else: - error = "Usernaam of wachtwoord onbekend, probeer opnieuw" - return render_template("login.html", error=error) + if request.method == "POST": + username = request.form["username"] + password = request.form["password"] + if db.cred.find_one({"username": username, "password": password}): + auth_user_id = db.cred.find_one({"username": username},{"uuid", 1}) + if auth_user_id: + app.logger.info(auth_user_id) + return render_template("welcome.html", id=auth_user_id["uuid"]) + else: + return render_template("404.html") + else: + error = "Usernaam of wachtwoord onbekend, probeer opnieuw" + return render_template("login.html", error=error) -@app.route("/welcome") -def welcome(id) +@app.route("/welcome/", methods=['GET']) +def welcome(id): + print("Hello: ", id) +# start server with run method +if __name__ == "__main__": + app.run(debug=True) \ No newline at end of file diff --git a/frontend/templates/404.html b/frontend/templates/404.html new file mode 100644 index 0000000..8fca942 --- /dev/null +++ b/frontend/templates/404.html @@ -0,0 +1,4 @@ +{% extends "base.html" %} +{% block content %} +

not found

+{% endblock %} \ No newline at end of file diff --git a/frontend/templates/index.html b/frontend/templates/index.html index e64d639..41a68be 100644 --- a/frontend/templates/index.html +++ b/frontend/templates/index.html @@ -11,7 +11,7 @@

Welkom bij Groclist

-
- + +
{% endblock %} diff --git a/frontend/templates/login.html b/frontend/templates/login.html index 2ab79f8..03938fe 100644 --- a/frontend/templates/login.html +++ b/frontend/templates/login.html @@ -6,7 +6,7 @@
-

Please login

+

Please login