From ba02d66a71c1c27b15938f09a8544cb6e042bc23 Mon Sep 17 00:00:00 2001 From: hok7z Date: Thu, 1 Sep 2022 16:24:40 +0300 Subject: [PATCH] Write simple login page --- apps/admin.py | 11 +++++------ apps/auth.py | 18 +++++++++++++++--- database/database.py | 1 + database/models.py | 10 ++++++---- templates/index.html | 0 templates/login.html | 8 +++++--- 6 files changed, 32 insertions(+), 16 deletions(-) delete mode 100644 templates/index.html diff --git a/apps/admin.py b/apps/admin.py index be55f0d..41c47a6 100644 --- a/apps/admin.py +++ b/apps/admin.py @@ -1,12 +1,11 @@ -from flask import Blueprint,render_template +from flask import Blueprint,render_template,session,redirect,url_for from werkzeug.security import generate_password_hash admin = Blueprint('admin',__name__) @admin.route("/admin") def admin_page(): - return render_template("admin.html") - -@admin.route("/new-web-user",methods=["POST"]) -def new_web_user(): - return "Create a new web user" + if ( "username" in session): + return render_template("admin.html",username=session["username"]) + + return redirect(url_for("login/")) diff --git a/apps/auth.py b/apps/auth.py index a119b05..2a10924 100644 --- a/apps/auth.py +++ b/apps/auth.py @@ -1,6 +1,8 @@ -from flask import Blueprint,render_template,request,redirect,url_for +from flask import Blueprint,render_template,request,redirect,url_for,session from werkzeug.security import check_password_hash +from database.models import WebUser + auth = Blueprint('auth',__name__) @auth.route("/") @@ -13,9 +15,19 @@ def login(): username = request.form.get("username") password = request.form.get("password") - # TODO: check hash password in database + if (not username) or (not password): + return "Enter username/password" + + # TODO: Write simple frontened (if username is None or password is None) + if (WebUser.userExists(username)): + user = WebUser.get(WebUser.username == username) - return "Some operations" + if ( check_password_hash(user.password_hash,password)): + session["username"] = username + return redirect(url_for("admin.admin_page")) + else: + return "Password not valid" + return "User not exist" @auth.route("/logout") def logout(): diff --git a/database/database.py b/database/database.py index e69de29..aa37ad0 100644 --- a/database/database.py +++ b/database/database.py @@ -0,0 +1 @@ +from models import WebUser diff --git a/database/models.py b/database/models.py index 12efdf8..f1438b3 100644 --- a/database/models.py +++ b/database/models.py @@ -14,14 +14,16 @@ class WebUser(Model): db_table = "webusers" database = db - def userExists(self,username) -> bool: + @staticmethod + def userExists(username) -> bool: """Check if the username exists in a database.""" query = WebUser.select().where(WebUser.username == username) - if (not query) or (not query.exists()): - return False + if (query): + if (query.exists()): + return True - return True + return False def build_models(): db.create_tables([WebUser]) diff --git a/templates/index.html b/templates/index.html deleted file mode 100644 index e69de29..0000000 diff --git a/templates/login.html b/templates/login.html index 89c87eb..f725739 100644 --- a/templates/login.html +++ b/templates/login.html @@ -1,3 +1,5 @@ - - -Login page +
+

+

+

+