File size: 2,773 Bytes
22edc61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import streamlit as st
from hashlib import sha256
from app import load_app
import os
import json
from dotenv import load_dotenv

load_dotenv()

# -----------------------
# Mock user database (replace with DB in production)
# -----------------------

USER_CONFIG = json.loads(os.getenv('USER_CONFIG'))

print("********************************************")
print(type(USER_CONFIG))
print(USER_CONFIG)
# -----------------------
# Authentication Utilities
# -----------------------
def hash_password(password: str) -> str:
    return sha256(password.encode()).hexdigest()

def is_authenticated(username: str, password: str) -> bool:
    if username in USER_CONFIG:
        return USER_CONFIG[username] == password
    return False

def login_page():
    st.markdown(
        """

        <h2 style="text-align:center;margin-bottom:30px;">πŸ” Secure Login</h2>

        """,
        unsafe_allow_html=True,
    )

    with st.form("login_form"):
        username = st.text_input("Username", placeholder="Enter username")
        password = st.text_input("Password", type="password", placeholder="Enter password")
        submit = st.form_submit_button("Login")

    if submit:
        if is_authenticated(username, password):
            st.session_state["auth"] = True
            st.session_state["user"] = username
            st.success("Login successful! Redirecting...")
            st.rerun()
        else:
            st.error("Invalid username or password")


def logout_button():
    # --- TOP CENTER WARNING ---
    st.markdown(
        """

        <div style="text-align:center; margin-top:-20px;">

            <p style="color:#5cb85c; font-size:20px; font-weight:bold;">

                Logged in as: <span style="color:black;">{}</span>

            </p>

        </div>

        """.format(st.session_state['user']),
        unsafe_allow_html=True
    )

    # --- TOP RIGHT LOGOUT BUTTON ---
    logout_col = st.columns([8, 2])  # Push button to the right
    with logout_col[1]:
        if st.button("Logout", key="logout_btn"):
            st.session_state["auth"] = False
            st.session_state["user"] = None
            st.rerun()


# -----------------------
# Main App (Protected)
# -----------------------
def main_app():
    logout_button()
    load_app()


# -----------------------
# Streamlit Entry Point
# -----------------------
def main():
    # Initialize session state
    if "auth" not in st.session_state:
        st.session_state["auth"] = False
        st.session_state["user"] = None

    # Render login or app
    if not st.session_state["auth"]:
        login_page()
    else:
        main_app()


if __name__ == "__main__":
    main()