Spaces:
Running
Running
| import streamlit as st | |
| from utils.utils import check_admin_login | |
| import asyncio | |
| import time | |
| def _index(): | |
| st.set_page_config( | |
| page_title="Auth Page", | |
| page_icon="π", | |
| layout="wide" | |
| ) | |
| # threading.Thread(target=__add_user_config, daemon=True).start() | |
| # print("[CONFIG] Background user config update started.") | |
| # -------------------------------------- | |
| # Initialize session state | |
| # -------------------------------------- | |
| if "logged_in" not in st.session_state: | |
| st.session_state.logged_in = False | |
| # -------------------------------------- | |
| # If user is logged in β show main app | |
| # -------------------------------------- | |
| if st.session_state.logged_in and st.session_state.admin_login: | |
| from admin_page import load_user_config | |
| load_user_config() | |
| st.stop() | |
| if st.session_state.logged_in: | |
| from app import load_app | |
| load_app() | |
| st.stop() # important: stop further execution | |
| # -------------------------------------- | |
| # Otherwise show login UI | |
| # -------------------------------------- | |
| st.sidebar.header("Options") | |
| page = st.sidebar.radio("Select Option", ["Login", "Sign Up", "Admin Login"],label_visibility="collapsed") | |
| # LOGIN PAGE | |
| if page == "Login": | |
| try: | |
| st.subheader("π Login") | |
| # New checkbox | |
| is_first_login = st.checkbox("Is First Login") | |
| # Common inputs | |
| username = st.text_input("Username") | |
| password = st.text_input("Password", type="password") | |
| # Conditional field | |
| token = None | |
| if is_first_login: | |
| token = st.text_input("Token") | |
| if st.button("Login"): | |
| from utils.utils import is_authenticated | |
| userinfo = { | |
| "username": username, | |
| "_access_key": password | |
| } | |
| # Add token only if first login | |
| if is_first_login: | |
| userinfo["token"] = token | |
| statuscode, message = asyncio.run(is_authenticated(userinfo=userinfo)) | |
| if statuscode == 0: | |
| st.session_state.admin_login=False | |
| st.session_state.logged_in = True | |
| st.session_state.user = username | |
| st.rerun() | |
| load_app() | |
| else: | |
| st.error(message) | |
| except Exception as e: | |
| st.error(e) | |
| st.error("Server Error while Login, please try after sometime") | |
| # Sign Up | |
| if page == "Sign Up": | |
| try: | |
| from utils.utils import _create_user | |
| st.header("π§Ύ Sign Up") | |
| # ---------- Reset ONLY when coming to this page ---------- | |
| if "current_page" not in st.session_state: | |
| st.session_state.current_page = "Sign Up" | |
| if st.session_state.current_page != "Sign Up": | |
| # reset fields BEFORE widgets are drawn | |
| st.session_state.email = "" | |
| st.session_state.username = "" | |
| st.session_state.password = "" | |
| st.session_state.current_page = "Sign Up" | |
| # ---------- Widgets ---------- | |
| email = st.text_input("Email", key="email") | |
| username = st.text_input("Username", key="username") | |
| password = st.text_input("Password", type="password", key="password") | |
| user_info = { | |
| "username": username, | |
| "email": email, | |
| "_access_key": password | |
| } | |
| all_filled = email.strip() and username.strip() and password.strip() | |
| if all_filled: | |
| if st.button("Create User"): | |
| statuscode, message = asyncio.run(_create_user(userinfo=user_info)) | |
| if statuscode == 0: | |
| st.success(message) | |
| time.sleep(3) | |
| # Reset BEFORE next rerun, not after widgets created | |
| st.session_state.current_page = "" | |
| st.rerun() | |
| else: | |
| st.error(message) | |
| else: | |
| st.button("Create User", disabled=True) | |
| except Exception as e: | |
| st.error(e) | |
| st.warning("Server is facing too many request, please try after sometime") | |
| if page == "Admin Login": | |
| try: | |
| st.subheader("π Admin Login") | |
| # Common inputs | |
| username = st.text_input("Username") | |
| password = st.text_input("Password", type="password") | |
| if st.button("Admin Login"): | |
| from utils.utils import check_admin_login | |
| userinfo = { | |
| "username": username, | |
| "password": password | |
| } | |
| statuscode, message = asyncio.run(check_admin_login(userinfo=userinfo)) | |
| if statuscode: | |
| st.session_state.logged_in = True | |
| st.session_state.admin_login = True | |
| st.session_state.user = username | |
| st.rerun() | |
| load_user_config() | |
| else: | |
| st.error(message) | |
| except Exception as e: | |
| st.error(e) | |
| st.error("Server Error while Login, please try after sometime") | |
| _index() |