WebAdminPanel/apps/auth.py

36 lines
1.1 KiB
Python
Raw Normal View History

2022-09-01 09:24:40 -04:00
from flask import Blueprint,render_template,request,redirect,url_for,session
2022-08-29 12:10:44 -04:00
from werkzeug.security import check_password_hash
2022-09-01 09:24:40 -04:00
from database.models import WebUser
2022-08-29 12:10:44 -04:00
auth = Blueprint('auth',__name__)
@auth.route("/")
@auth.route("/login",methods=["GET"])
def login_template():
return render_template("login.html")
@auth.route("/login",methods=["POST"])
def login():
username = request.form.get("username")
password = request.form.get("password")
2022-09-01 09:24:40 -04:00
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)
2022-08-29 12:10:44 -04:00
2022-09-01 09:24:40 -04:00
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"
2022-08-29 12:10:44 -04:00
@auth.route("/logout")
def logout():
# Logout from account
return redirect(url_for("auth.login"))