File size: 2,481 Bytes
4c46ca1 23cf709 d930228 4c46ca1 23cf709 aec8fff d930228 23cf709 aec8fff 23cf709 d930228 aec8fff d930228 23cf709 d930228 23cf709 aec8fff 23cf709 aec8fff 23cf709 d930228 23cf709 95805de 23cf709 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | 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")
# Redirect if already authenticated
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()
# Create page objects with auth manager dependency
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:"),
}
|