Spaces:
Sleeping
Sleeping
| """Authentication logic for the Audio Labeling Tool.""" | |
| import logging | |
| import streamlit as st | |
| from config import load_config | |
| logger = logging.getLogger(__name__) | |
| def authenticate(username: str, password: str) -> str | None: | |
| """Validate credentials against config. | |
| Returns: | |
| "admin" if admin credentials, "labeler" if labeler credentials, None if invalid. | |
| """ | |
| config = load_config() | |
| # Check admin | |
| admin_cfg = config["admin"] | |
| if username == admin_cfg["username"] and password == admin_cfg["password"]: | |
| return "admin" | |
| # Check labelers | |
| labeler = config["labelers"].get(username) | |
| if labeler is not None and labeler["password"] == password: | |
| return "labeler" | |
| return None | |
| def login(username: str, role: str) -> None: | |
| """Set session state on successful login.""" | |
| st.session_state["authenticated"] = True | |
| st.session_state["username"] = username | |
| st.session_state["role"] = role | |
| logger.info(f"User '{username}' logged in as '{role}'.") | |
| def logout() -> None: | |
| """Clear all session state and return to login.""" | |
| username = st.session_state.get("username", "unknown") | |
| for key in list(st.session_state.keys()): | |
| del st.session_state[key] | |
| st.session_state["authenticated"] = False | |
| logger.info(f"User '{username}' logged out.") | |