From 8d7eefb2e2648f759e33a9c8bfb3381b885d43b0 Mon Sep 17 00:00:00 2001 From: ditaajipratama Date: Wed, 19 Jun 2024 00:18:53 +0700 Subject: [PATCH] Login and Session features --- app/handler.py | 31 +++++++++++++++++++++++++++++++ app/modules/api/auth.py | 2 -- app/modules/public/login.py | 26 ++++++++++++++++++++++++++ app/pages/public/login.html | 13 +++++++++++++ app/static/js/auth/login.js | 2 +- 5 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 app/modules/public/login.py create mode 100644 app/pages/public/login.html diff --git a/app/handler.py b/app/handler.py index 1932390..cd2dfbd 100644 --- a/app/handler.py +++ b/app/handler.py @@ -17,6 +17,7 @@ import modules.public.home as public_home import modules.public.register as public_register import modules.public.notme as public_notme import modules.public.verify as public_verify +import modules.public.login as public_login import modules.api.auth as api_auth @@ -59,6 +60,15 @@ def index(): } return public_verify.verify().html(params) +@app.route('/login') +def index(): + params = { + "mako" : { + "website" : template_public.main(directory.page["public"], "login") + } + } + return public_login.login().html(params) + @app.route('/api/auth/registration/register/', method='POST') def index(roles): try: @@ -113,3 +123,24 @@ def index(): except Exception as e: print(str(e)) return json.dumps({}, indent = 2).encode() + +@app.route('/api/auth/login', method='POST') +def index(): + try: + params = request.json + response.content_type = 'application/json' + return json.dumps(api_auth.auth().login(params), indent = 2).encode() + except Exception as e: + print(str(e)) + return json.dumps({}, indent = 2).encode() + +@app.route('/api/auth/session/', method='POST') +def index(type): + try: + params = request.json + params["type"] = type + response.content_type = 'application/json' + return json.dumps(api_auth.auth().session(params), indent = 2).encode() + except Exception as e: + print(str(e)) + return json.dumps({}, indent = 2).encode() diff --git a/app/modules/api/auth.py b/app/modules/api/auth.py index e32b94c..91b5e8e 100644 --- a/app/modules/api/auth.py +++ b/app/modules/api/auth.py @@ -419,10 +419,8 @@ class auth: response = {} type = params["type" ] # set / check / out jwt = params["jwt" ] - payload = tokenguard.decode(jwt, globalvar.ssh['key']['public']) session_id = payload["session"]["id"] - try: session_beaker = request.environ.get('beaker.session') if type == 'set': diff --git a/app/modules/public/login.py b/app/modules/public/login.py new file mode 100644 index 0000000..4c7731a --- /dev/null +++ b/app/modules/public/login.py @@ -0,0 +1,26 @@ +import mysql.connector as mariadb +from mako.template import Template +from config import globalvar, database + +class login: + + def __init__(self): + self.db_main = mariadb.connect(**database.db_main) + self.cursor = self.db_main.cursor(dictionary=True) + self.user_roles = [0] # Cari user roles disini + + def html(self, params): + active_page = "Login" + return Template(params["mako"]["website"]['index']).render( + title = globalvar.title, + header = globalvar.header, + navbar = Template(params["mako"]["website"]['navbar']).render( + menu = globalvar.menu['public']['navbar'], + user_roles = self.user_roles, + active_page = active_page + ), + footer = Template(params["mako"]["website"]['footer']).render( + copyright = globalvar.copyright, + ), + container = Template(params["mako"]["website"]['container']).render() + ) diff --git a/app/pages/public/login.html b/app/pages/public/login.html new file mode 100644 index 0000000..864530a --- /dev/null +++ b/app/pages/public/login.html @@ -0,0 +1,13 @@ +

Login

+ + + +
+
+ + + + + diff --git a/app/static/js/auth/login.js b/app/static/js/auth/login.js index cf5cb5a..e37efe0 100644 --- a/app/static/js/auth/login.js +++ b/app/static/js/auth/login.js @@ -21,7 +21,7 @@ function responseSession(response) { document.getElementById("alert-desc" ).innerHTML = "Welcome!"; document.getElementById("alert-response").classList.add('alert-success'); document.getElementById("alert-response").style.display = 'block'; - window.location.replace("/portal/dashboard?msg=Welcome"); + window.location.replace("/?msg=Welcome"); } else { document.getElementById("alert-desc" ).innerHTML = "Internal error";