File size: 6,573 Bytes
6081f39
 
 
73d90ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2559b31
73d90ba
 
 
 
 
 
 
6081f39
73d90ba
6081f39
73d90ba
 
 
2559b31
73d90ba
2559b31
73d90ba
 
2559b31
73d90ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2559b31
73d90ba
 
 
 
 
2559b31
73d90ba
 
 
 
2559b31
73d90ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6081f39
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
102
103
104
105
106
import streamlit as st
from datetime import datetime

# Import database functions directly here.
# user_management.py is now responsible for handling its own database interactions.
# It should NOT import anything from app.py
# Assuming these functions are defined in database.py
try:
    from database import register_user, user_exists, check_user_credentials, save_user_history, get_user_history
except ImportError:
    st.error("Could not import database functions in user_management.py. Please ensure database.py exists and contains required functions.")
    # Define dummy functions to allow the app to run without a real database.py for now
    def get_user_history(user_id):
        st.warning("Database function 'get_user_history' not implemented. History will not be persistent.")
        return []
    def register_user(user_id, full_name, dob, email, password):
        st.warning("Database function 'register_user' not implemented. User registration will not be persistent.")
        return True # Simulate success
    def user_exists(user_id):
        st.warning("Database function 'user_exists' not implemented. User existence check will not work.")
        return False # Simulate user not existing
    def check_user_credentials(user_id, password):
        st.warning("Database function 'check_user_credentials' not implemented. Login will not work.")
        return False # Simulate login failure
    def save_user_history(user_id, symptoms, predicted_diseases):
        st.warning("Database function 'save_user_history' not implemented. History saving will not be persistent.")
        pass


def render_user_management_sidebar(session_state):
    """
    Handles user registration and login forms in the sidebar.
    Receives the Streamlit session_state object to manage user login status.
    """
    st.sidebar.header("User Management πŸ§‘β€βš•οΈ")

    # Check if a user is currently logged in via session_state
    if session_state.get("user_id"):
        st.sidebar.write(f"Welcome, {session_state.user_id}!")
        if st.sidebar.button("Logout", key="logout_button_sidebar"): # Unique key for sidebar button
            session_state.user_id = None
            session_state.logged_in_user = None # Clear this too upon logout
            st.success("Logged out successfully!")
            st.rerun() # Rerun to update UI immediately
    else:
        # Toggle between sign-in and register forms
        form_type = st.sidebar.selectbox("Choose Action", ["Sign In", "Register"], key="form_type_sidebar")

        if form_type == "Sign In":
            with st.sidebar.form("login_form_sidebar"): # Unique form key
                st.markdown("### Sign In")
                login_user_id = st.text_input("User ID", key="login_user_id_input_sidebar") # Unique widget key
                login_password = st.text_input("Password", type="password", key="login_password_input_sidebar") # Unique widget key
                submit_login = st.form_submit_button("Sign In")

                if submit_login:
                    if not login_user_id or not login_password:
                        st.sidebar.error("Please enter both User ID and Password.")
                    else:
                        with st.spinner("Signing in..."): # Spinner for login
                            if check_user_credentials(login_user_id, login_password):
                                session_state.user_id = login_user_id
                                session_state.logged_in_user = login_user_id # Set logged_in_user in session state
                                st.sidebar.success("Signed in successfully!")
                                st.rerun() # Rerun to update UI with logged-in state
                            else:
                                st.sidebar.error("Invalid User ID or Password.")
        else: # Register
            with st.sidebar.form("register_form_sidebar"): # Unique form key
                st.markdown("### Register")
                reg_user_id = st.text_input("Choose a unique User ID", key="reg_user_id_input_sidebar") # Unique widget key
                full_name = st.text_input("Full Name", key="reg_full_name_input_sidebar") # Unique widget key
                dob = st.date_input("Date of Birth", min_value=datetime(1900, 1, 1), max_value=datetime.today(), key="reg_dob_input_sidebar") # Unique widget key
                email = st.text_input("Email (optional)", key="reg_email_input_sidebar") # Unique widget key
                reg_password = st.text_input("Password", type="password", key="reg_password_input_sidebar") # Unique widget key
                submit_register = st.form_submit_button("Register")

                if submit_register:
                    if not reg_user_id or not reg_password or not full_name:
                        st.sidebar.error("User ID, Full Name, and Password are required.")
                    else:
                        with st.spinner("Checking User ID..."): # Spinner for user_exists check
                            if user_exists(reg_user_id):
                                st.sidebar.error("User ID already taken. Please choose another.")
                            else:
                                with st.spinner("Registering user..."): # Spinner for registration
                                    # Pass dob as a datetime.date object; database function should handle conversion if needed
                                    if register_user(reg_user_id, full_name, dob, email or None, reg_password):
                                        session_state.user_id = reg_user_id
                                        session_state.logged_in_user = reg_user_id # Set logged_in_user in session state
                                        st.sidebar.success("Registered successfully! You are now logged in.")
                                        st.rerun() # Rerun to update UI with logged-in state
                                    else:
                                        st.sidebar.error("Registration failed. Please try again.")

def save_history_to_db_if_logged_in(user_id: str, symptoms: str, predicted_diseases: str):
    """
    Saves user interaction history to the database.
    This function is called from app.py when a user is logged in.
    It directly calls the database function to save data.
    """
    # This function assumes user_id is already validated (i.e., user is logged in)
    with st.spinner("Saving history..."): # Spinner for saving history
        save_user_history(user_id, symptoms, predicted_diseases)