import streamlit as st from hashlib import sha256 from app import load_app import os import json from dotenv import load_dotenv load_dotenv() # ----------------------- # Mock user database (replace with DB in production) # ----------------------- USER_CONFIG = json.loads(os.getenv('USER_CONFIG')) print("********************************************") print(type(USER_CONFIG)) print(USER_CONFIG) # ----------------------- # Authentication Utilities # ----------------------- def hash_password(password: str) -> str: return sha256(password.encode()).hexdigest() def is_authenticated(username: str, password: str) -> bool: if username in USER_CONFIG: return USER_CONFIG[username] == password return False def login_page(): st.markdown( """

🔐 Secure Login

""", unsafe_allow_html=True, ) with st.form("login_form"): username = st.text_input("Username", placeholder="Enter username") password = st.text_input("Password", type="password", placeholder="Enter password") submit = st.form_submit_button("Login") if submit: if is_authenticated(username, password): st.session_state["auth"] = True st.session_state["user"] = username st.success("Login successful! Redirecting...") st.rerun() else: st.error("Invalid username or password") def logout_button(): # --- TOP CENTER WARNING --- st.markdown( """

Logged in as: {}

""".format(st.session_state['user']), unsafe_allow_html=True ) # --- TOP RIGHT LOGOUT BUTTON --- logout_col = st.columns([8, 2]) # Push button to the right with logout_col[1]: if st.button("Logout", key="logout_btn"): st.session_state["auth"] = False st.session_state["user"] = None st.rerun() # ----------------------- # Main App (Protected) # ----------------------- def main_app(): logout_button() load_app() # ----------------------- # Streamlit Entry Point # ----------------------- def main(): # Initialize session state if "auth" not in st.session_state: st.session_state["auth"] = False st.session_state["user"] = None # Render login or app if not st.session_state["auth"]: login_page() else: main_app() if __name__ == "__main__": main()