File size: 1,459 Bytes
c917d47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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