build(ui): separating admin + user ui

This commit is contained in:
Krrish Dholakia 2023-12-25 22:51:35 +05:30
parent d85c19394f
commit e0ea2aa147
4 changed files with 71 additions and 28 deletions

View file

@ -7,8 +7,8 @@ load_dotenv()
import streamlit as st
import base64, binascii, os
from admin import admin_page
from auth import auth_page
from urllib.parse import urlparse, parse_qs
from auth import auth_page, verify_with_otp
import urllib.parse
# Parse the query params in the URL
@ -31,11 +31,10 @@ def is_base64(sb):
return False
# Check if decoding is successful.
# The result of the decode is not required, so it is ignored.
_ = base64.urlsafe_b64decode(sb_bytes)
decoded_params = base64.urlsafe_b64decode(sb_bytes)
# If the decode was successful, the input is likely base64
return True
return True, decoded_params
except (binascii.Error, ValueError):
# If an error occurs, return False, as the input is not base64
return False
@ -43,17 +42,46 @@ def is_base64(sb):
# Check the URL path and route to the correct page based on the path
query_params = get_query_params()
print(f"query_params: {query_params}")
page_param = query_params.get("page", [None])[0]
# Route to the appropriate page based on the URL query param
if page_param:
token_hash = query_params.get("token_hash", [None])[0]
decoded_token = None
if token_hash is not None:
print(f"token_hash: {token_hash}")
decoded_token = verify_with_otp(token=token_hash)
print(f"decoded_token: {decoded_token}")
if page_param is not None:
try:
print(f"page_param: {page_param}")
# Try to decode the page_param from base64
if is_base64(page_param):
auth_page(redirect_url=f"{os.getenv('BASE_URL')}/{page_param}")
is_valid, decoded_params = is_base64(page_param)
print(f"is_valid: {is_valid}; decoded_params: {decoded_params}")
if is_valid:
if decoded_token is None:
auth_page(page_param=page_param)
else:
# Convert the bytes to a string
params_str = decoded_params.decode("utf-8")
# Parse the parameters
params = urllib.parse.parse_qs(params_str)
# Extract the value of admin_emails
admin_emails = params.get("admin_emails", [""])[0].split(",")
print(admin_emails)
print(vars(decoded_token.user))
if decoded_token.user.email in admin_emails:
# admin ui
admin_page(is_admin=True)
else:
# user ui
st.write(
f"email: {decoded_token.user.email}; admin_emails: {admin_emails}"
)
else:
st.error("Unknown page")
except Exception as e:
st.error("Failed to decode the page parameter. Error: " + str(e))
else:
admin_page()
admin_page(is_admin=False)