File size: 3,928 Bytes
5e51056
4f98e67
 
5e51056
4f98e67
 
5e51056
4f98e67
 
 
 
 
 
 
 
 
 
 
5e51056
4f98e67
 
 
 
 
 
 
 
 
 
5e51056
4f98e67
 
 
 
 
 
 
 
5e51056
4f98e67
5e51056
4f98e67
5e51056
4f98e67
 
5e51056
4f98e67
 
 
8a5a9ad
 
4f98e67
 
 
 
 
 
 
 
 
 
5e51056
4f98e67
 
 
 
 
 
 
5e51056
4f98e67
 
 
 
 
 
 
 
 
 
 
 
 
 
8a5a9ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f98e67
 
5e51056
 
 
4f98e67
8a5a9ad
 
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
107
108
109
110
111
112
113
114
115
116
import streamlit as st
import sqlite3
import hashlib

# Database setup
DB_FILE = "users.db"

def create_user_table():
    conn = sqlite3.connect(DB_FILE)
    cursor = conn.cursor()
    cursor.execute("""
        CREATE TABLE IF NOT EXISTS users (
            username TEXT PRIMARY KEY,
            password TEXT
        )
    """)
    conn.commit()
    conn.close()

def add_user(username, password):
    conn = sqlite3.connect(DB_FILE)
    cursor = conn.cursor()
    hashed_password = hashlib.sha256(password.encode()).hexdigest()
    try:
        cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, hashed_password))
        conn.commit()
    except sqlite3.IntegrityError:
        st.error("Username already exists. Please choose a different username.")
    conn.close()

def authenticate_user(username, password):
    conn = sqlite3.connect(DB_FILE)
    cursor = conn.cursor()
    hashed_password = hashlib.sha256(password.encode()).hexdigest()
    cursor.execute("SELECT * FROM users WHERE username = ? AND password = ?", (username, hashed_password))
    user = cursor.fetchone()
    conn.close()
    return user

# Main application
def main():
    st.title("Welcome to Your Streamlit App")

    # Initialize database
    create_user_table()

    # Authentication state
    if "authenticated" not in st.session_state:
        st.session_state.authenticated = False
    if "username" not in st.session_state:
        st.session_state.username = None

    # Central login/register interface
    if not st.session_state.authenticated:
        st.subheader("Please Log In or Register to Continue")
        auth_mode = st.radio("Choose an Option", ["Log In", "Register"], horizontal=True)

        if auth_mode == "Log In":
            st.subheader("Log In")
            username = st.text_input("Username", key="login_username")
            password = st.text_input("Password", type="password", key="login_password")

            if st.button("Log In"):
                if authenticate_user(username, password):
                    st.success(f"Welcome back, {username}!")
                    st.session_state.authenticated = True
                    st.session_state.username = username
                else:
                    st.error("Invalid username or password. Please try again.")

        elif auth_mode == "Register":
            st.subheader("Register")
            username = st.text_input("Create Username", key="register_username")
            password = st.text_input("Create Password", type="password", key="register_password")

            if st.button("Register"):
                if username and password:
                    add_user(username, password)
                    st.success("Account created successfully! You can now log in.")
                else:
                    st.error("Please fill in all fields.")

    # Authenticated user workspace
    if st.session_state.authenticated:
        # Display logout button in the upper-right corner
        with st.container():
            st.markdown(
                """
                <style>
                .logout-button {
                    position: fixed;
                    top: 15px;
                    right: 15px;
                }
                </style>
                """,
                unsafe_allow_html=True,
            )
            if st.button("Log Out", key="logout"):
                # Show a confirmation dialog
                if st.button("Confirm Logout"):
                    st.session_state.authenticated = False
                    st.session_state.username = None
                    st.success("You have been logged out.")
                    st.experimental_rerun()

        # Workspace content
        st.subheader(f"Hello, {st.session_state.username}!")
        st.write("This is your workspace. All your saved work will appear here.")

if __name__ == "__main__":
    main()