Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,7 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
|
3 |
# Initialize session state
|
4 |
if 'username' not in st.session_state:
|
@@ -6,9 +9,54 @@ if 'username' not in st.session_state:
|
|
6 |
if 'scratch_pad' not in st.session_state:
|
7 |
st.session_state.scratch_pad = ''
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
st.session_state.username = username
|
11 |
-
st.session_state.scratch_pad = scratch_pad
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
st.title("Welcome to My App")
|
14 |
|
@@ -16,8 +64,16 @@ if st.session_state.username:
|
|
16 |
st.subheader(f"Welcome back, {st.session_state.username}!")
|
17 |
scratch_pad = st.text_area("Scratch Pad", value=st.session_state.scratch_pad, height=200)
|
18 |
if st.button("πΎ Save"):
|
19 |
-
save_session()
|
|
|
|
|
20 |
else:
|
21 |
-
username = st.text_input("
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
from cryptography.fernet import Fernet
|
5 |
|
6 |
# Initialize session state
|
7 |
if 'username' not in st.session_state:
|
|
|
9 |
if 'scratch_pad' not in st.session_state:
|
10 |
st.session_state.scratch_pad = ''
|
11 |
|
12 |
+
# Load or create user history file
|
13 |
+
history_file = 'user_history.json'
|
14 |
+
if not os.path.exists(history_file):
|
15 |
+
with open(history_file, 'w') as file:
|
16 |
+
json.dump({}, file)
|
17 |
+
|
18 |
+
# Load user history
|
19 |
+
with open(history_file, 'r') as file:
|
20 |
+
user_history = json.load(file)
|
21 |
+
|
22 |
+
# Generate encryption key
|
23 |
+
key = Fernet.generate_key()
|
24 |
+
fernet = Fernet(key)
|
25 |
+
|
26 |
+
def save_session(username, password):
|
27 |
st.session_state.username = username
|
28 |
+
st.session_state.scratch_pad = st.session_state.scratch_pad
|
29 |
+
encrypted_password = fernet.encrypt(password.encode()).decode()
|
30 |
+
user_history[username] = encrypted_password
|
31 |
+
with open(history_file, 'w') as file:
|
32 |
+
json.dump(user_history, file)
|
33 |
+
|
34 |
+
def register_user(username, password):
|
35 |
+
if username not in user_history:
|
36 |
+
save_session(username, password)
|
37 |
+
st.success("User registered successfully!")
|
38 |
+
else:
|
39 |
+
st.warning("Username already exists. Please choose a different username.")
|
40 |
+
|
41 |
+
def login_user(username, password):
|
42 |
+
if username in user_history:
|
43 |
+
encrypted_password = user_history[username]
|
44 |
+
decrypted_password = fernet.decrypt(encrypted_password.encode()).decode()
|
45 |
+
if password == decrypted_password:
|
46 |
+
save_session(username, password)
|
47 |
+
st.success("Logged in successfully!")
|
48 |
+
else:
|
49 |
+
st.error("Invalid password. Please try again.")
|
50 |
+
else:
|
51 |
+
st.warning("Username not found. Please register first.")
|
52 |
+
|
53 |
+
def reset_password(username):
|
54 |
+
if username in user_history:
|
55 |
+
new_password = Fernet.generate_password(8).decode()
|
56 |
+
save_session(username, new_password)
|
57 |
+
st.success(f"Password reset successful. Your new password is: {new_password}")
|
58 |
+
else:
|
59 |
+
st.warning("Username not found.")
|
60 |
|
61 |
st.title("Welcome to My App")
|
62 |
|
|
|
64 |
st.subheader(f"Welcome back, {st.session_state.username}!")
|
65 |
scratch_pad = st.text_area("Scratch Pad", value=st.session_state.scratch_pad, height=200)
|
66 |
if st.button("πΎ Save"):
|
67 |
+
save_session(st.session_state.username, '')
|
68 |
+
if st.button("π Reset Password"):
|
69 |
+
reset_password(st.session_state.username)
|
70 |
else:
|
71 |
+
username = st.text_input("Username:")
|
72 |
+
password = st.text_input("Password:", type="password")
|
73 |
+
col1, col2 = st.columns(2)
|
74 |
+
with col1:
|
75 |
+
if st.button("π Login"):
|
76 |
+
login_user(username, password)
|
77 |
+
with col2:
|
78 |
+
if st.button("π Register"):
|
79 |
+
register_user(username, password)
|