File size: 2,178 Bytes
dd48f96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
879e336
dd48f96
879e336
dd48f96
 
 
879e336
dd48f96
 
 
 
 
 
 
 
 
 
 
 
 
 
879e336
dd48f96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import hmac


def _check_password():
    """Authenticate user and manage login state."""
    if st.session_state.get("authenticated"):
        return True

    with st.form("Credentials"):
        username = st.text_input("Username")
        password = st.text_input("Password", type="password")
        submitted = st.form_submit_button("Log in")

    if submitted:
        if username == st.secrets["username"] and hmac.compare_digest(
            password,
            st.secrets["password"],
        ):
            st.session_state.authenticated = True
            st.session_state.username = username
            # st.session_state.role = st.secrets.roles.get(username, "user")  # Default to "user" if role not specified
            st.rerun()
        else:
            st.error("User not known or password incorrect.")

    return False


def _authenticated_menu():
    # Show a navigation menu for authenticated users
    st.sidebar.page_link("app.py", label="Home Page", icon="🏑")
    # st.sidebar.page_link("pages/composer.py", label="Insight Composer", icon ="🎨")
    # st.sidebar.page_link("pages/user.py", label="Regular User Page", icon="🚎")
    st.sidebar.divider()
    st.sidebar.write(f"Welcome, {st.session_state.username}!")
    # st.sidebar.write(f"Your role is: {st.session_state.role}")
    if st.sidebar.button("Logout"):
        _logout()


def _unauthenticated_menu():
    # Show a navigation menu for unauthenticated users
    # st.sidebar.page_link("home.py", label="Log in")
    pass

def _logout():
    """Log out the current user."""
    st.session_state.clear()
    st.success("You have been logged out successfully.")
    st.rerun()


def menu():
    # Determine if a user is logged in or not, then show the correct
    if not _check_password():
        _unauthenticated_menu()
        st.stop()
    else:
        _authenticated_menu()


def menu_with_redirect():
    # Redirect users to the main page if not logged in, otherwise continue to
    # render the navigation menu
    if not _check_password():
        st.switch_page("app.py")
    menu()