File size: 3,045 Bytes
5e51056
4f98e67
 
5e51056
4f98e67
 
5e51056
4f98e67
 
 
 
 
 
 
 
 
 
 
5e51056
4f98e67
 
 
 
 
 
 
 
 
 
5e51056
4f98e67
 
 
 
 
 
 
 
5e51056
4f98e67
5e51056
4f98e67
5e51056
4f98e67
 
5e51056
4f98e67
 
 
 
 
 
 
 
 
 
 
 
 
5e51056
4f98e67
 
 
 
 
 
 
5e51056
4f98e67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e51056
 
 
4f98e67
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
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

    # 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:
        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()