Spaces:
Sleeping
Sleeping
import os | |
import streamlit as st | |
def check_credentials(): | |
"""Handles login form and returns True if authenticated successfully.""" | |
# Check if already authenticated | |
if st.session_state.get("authenticated", False): | |
return True # User is already authenticated | |
# Retrieve credentials from environment variables (set via Hugging Face Secrets) | |
expected_username = os.environ.get("APP_USERNAME") | |
expected_password = os.environ.get("APP_PASSWORD") | |
if not expected_username or not expected_password: | |
st.error("Server is misconfigured: missing credentials.") | |
return False | |
# Show the login form only if not authenticated | |
with st.form("login_form", clear_on_submit=True): | |
st.text_input("Username", key="username") | |
st.text_input("Password", type="password", key="password") | |
submit_button = st.form_submit_button("Login") | |
if submit_button: | |
# Validate credentials | |
if ( | |
st.session_state["username"] == expected_username | |
and st.session_state["password"] == expected_password | |
): | |
st.session_state["authenticated"] = True # Mark user as authenticated | |
return True | |
else: | |
st.error("π Incorrect username or password") | |
return False # Indicate failed authentication | |
# Return False if login not attempted or failed | |
return False | |