Write simple login page

This commit is contained in:
hok7z 2022-09-01 16:24:40 +03:00
parent a42283e3eb
commit ba02d66a71
6 changed files with 32 additions and 16 deletions

View File

@ -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")
if ( "username" in session):
return render_template("admin.html",username=session["username"])
@admin.route("/new-web-user",methods=["POST"])
def new_web_user():
return "Create a new web user"
return redirect(url_for("login/"))

View File

@ -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"
return "Some operations"
# TODO: Write simple frontened (if username is None or password is None)
if (WebUser.userExists(username)):
user = WebUser.get(WebUser.username == username)
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():

View File

@ -0,0 +1 @@
from models import WebUser

View File

@ -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 False
def build_models():
db.create_tables([WebUser])

View File

View File

@ -1,3 +1,5 @@
<!-- Login form -->
Login page
<form action="/login" method="post">
<p><input type="text" name="username" placeholder="Enter username"></p>
<p><input type="password" name="password" placeholder="Enter password"></p>
<p><button type="sumbit">Log In</button></p>
</form>