Login and Session features

This commit is contained in:
Dita Aji Pratama 2024-06-19 00:18:53 +07:00
parent b64f53ea0e
commit 8d7eefb2e2
5 changed files with 71 additions and 3 deletions

View File

@ -17,6 +17,7 @@ import modules.public.home as public_home
import modules.public.register as public_register import modules.public.register as public_register
import modules.public.notme as public_notme import modules.public.notme as public_notme
import modules.public.verify as public_verify import modules.public.verify as public_verify
import modules.public.login as public_login
import modules.api.auth as api_auth import modules.api.auth as api_auth
@ -59,6 +60,15 @@ def index():
} }
return public_verify.verify().html(params) 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/<roles>', method='POST') @app.route('/api/auth/registration/register/<roles>', method='POST')
def index(roles): def index(roles):
try: try:
@ -113,3 +123,24 @@ def index():
except Exception as e: except Exception as e:
print(str(e)) print(str(e))
return json.dumps({}, indent = 2).encode() 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/<type>', 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()

View File

@ -419,10 +419,8 @@ class auth:
response = {} response = {}
type = params["type" ] # set / check / out type = params["type" ] # set / check / out
jwt = params["jwt" ] jwt = params["jwt" ]
payload = tokenguard.decode(jwt, globalvar.ssh['key']['public']) payload = tokenguard.decode(jwt, globalvar.ssh['key']['public'])
session_id = payload["session"]["id"] session_id = payload["session"]["id"]
try: try:
session_beaker = request.environ.get('beaker.session') session_beaker = request.environ.get('beaker.session')
if type == 'set': if type == 'set':

View File

@ -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()
)

View File

@ -0,0 +1,13 @@
<h1>Login</h1>
<script type="text/javascript" src="/js/carrack.js"></script>
<input required type="text" id="form-username" placeholder="Username" > <br>
<input required type="password" id="form-password" placeholder="Password" > <br>
<button type="button" onclick="onSubmit()">Login</button>
<div id="alert-response" role="alert">
<b id="alert-status">Loading...</b> <span id="alert-desc">Please wait...</span>
</div>
<script type="text/javascript" src="/js/auth/login.js"></script>

View File

@ -21,7 +21,7 @@ function responseSession(response) {
document.getElementById("alert-desc" ).innerHTML = "Welcome!"; document.getElementById("alert-desc" ).innerHTML = "Welcome!";
document.getElementById("alert-response").classList.add('alert-success'); document.getElementById("alert-response").classList.add('alert-success');
document.getElementById("alert-response").style.display = 'block'; document.getElementById("alert-response").style.display = 'block';
window.location.replace("/portal/dashboard?msg=Welcome"); window.location.replace("/?msg=Welcome");
} }
else { else {
document.getElementById("alert-desc" ).innerHTML = "Internal error"; document.getElementById("alert-desc" ).innerHTML = "Internal error";