| import streamlit as st |
|
|
| from config import AppConfig |
| from pages import log_page_visit |
|
|
|
|
| class AuthManager: |
| def __init__(self, config: AppConfig): |
| self.config = config |
|
|
| def check_admin_access(self) -> bool: |
| """Check if admin access is granted""" |
| if not self.config.ADMIN_PASSWORD: |
| return False |
| return st.session_state.get("admin_authenticated", False) |
|
|
| def is_logged_in(self) -> bool: |
| """Check if user is logged in""" |
| return st.session_state.get("logged_in", False) and st.session_state.get( |
| "admin_authenticated", False |
| ) |
|
|
| def authenticate(self, username: str, password: str) -> bool: |
| """Authenticate user credentials""" |
| return ( |
| username == "admin" |
| and password == self.config.ADMIN_PASSWORD |
| and bool(self.config.ADMIN_PASSWORD) |
| ) |
|
|
|
|
| @log_page_visit(page_name="Login") |
| def login(auth_manager: AuthManager): |
| """Handle admin login with proper configuration and state management""" |
| st.title("Login") |
|
|
| |
| if auth_manager.check_admin_access(): |
| st.success("Already logged in!") |
| return |
|
|
| with st.form("login_form"): |
| username = st.text_input("Username") |
| password = st.text_input("Password", type="password") |
| submit = st.form_submit_button("Log in") |
|
|
| if submit: |
| if auth_manager.authenticate(username, password): |
| st.session_state.update( |
| {"logged_in": True, "admin_authenticated": True} |
| ) |
| st.success("Login successful!") |
| st.rerun() |
| else: |
| if not auth_manager.config.ADMIN_PASSWORD: |
| st.error("Admin access is not configured") |
| else: |
| st.error("Invalid username or password") |
|
|
|
|
| def logout(): |
| """Handle admin logout""" |
| if st.button("Log out"): |
| st.session_state.update({"logged_in": False, "admin_authenticated": False}) |
| st.rerun() |
|
|
|
|
| |
| def create_admin_pages(config: AppConfig): |
| auth_manager = AuthManager(config) |
| return { |
| "login": st.Page( |
| lambda: login(auth_manager), |
| title="Log in", |
| url_path="/login", |
| icon=":material/login:", |
| ), |
| "logout": st.Page(logout, title="Log out", icon=":material/logout:"), |
| } |
|
|