Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import streamlit as st
|
|
| 4 |
|
| 5 |
# Set page config
|
| 6 |
st.set_page_config(
|
| 7 |
-
page_title="
|
| 8 |
page_icon="💳",
|
| 9 |
layout="wide",
|
| 10 |
)
|
|
@@ -130,31 +130,66 @@ if get_env_var('PLAID_CLIENT_ID') and get_env_var('PLAID_SECRET'):
|
|
| 130 |
else:
|
| 131 |
plaid_client = None
|
| 132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
# JWT Authentication
|
| 134 |
def authenticate_user():
|
| 135 |
st.sidebar.title("User Authentication")
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
if
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
st.
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
if 'auth_token' not in st.session_state:
|
| 150 |
authenticate_user()
|
| 151 |
else:
|
| 152 |
# Proceed with the application
|
| 153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
# Title of the app
|
| 155 |
-
st.title("
|
| 156 |
st.sidebar.header("Navigation")
|
| 157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
# Main application navigation
|
| 159 |
service_choice = st.sidebar.selectbox("Choose a Service", [
|
| 160 |
"Dashboard",
|
|
|
|
| 4 |
|
| 5 |
# Set page config
|
| 6 |
st.set_page_config(
|
| 7 |
+
page_title="Quantum Bank: Invented by James Burvel O'Callaghan III",
|
| 8 |
page_icon="💳",
|
| 9 |
layout="wide",
|
| 10 |
)
|
|
|
|
| 130 |
else:
|
| 131 |
plaid_client = None
|
| 132 |
|
| 133 |
+
# Initialize user database (In-memory for demonstration purposes)
|
| 134 |
+
if 'user_database' not in st.session_state:
|
| 135 |
+
st.session_state['user_database'] = {}
|
| 136 |
+
|
| 137 |
# JWT Authentication
|
| 138 |
def authenticate_user():
|
| 139 |
st.sidebar.title("User Authentication")
|
| 140 |
+
auth_option = st.sidebar.radio("Select an option", ["Login", "Create Account"])
|
| 141 |
+
|
| 142 |
+
if auth_option == "Login":
|
| 143 |
+
username = st.sidebar.text_input("Username")
|
| 144 |
+
password = st.sidebar.text_input("Password", type="password")
|
| 145 |
+
if st.sidebar.button("Login"):
|
| 146 |
+
# Hash the password
|
| 147 |
+
hashed_password = hashlib.sha256(password.encode()).hexdigest()
|
| 148 |
+
# Check credentials
|
| 149 |
+
user_db = st.session_state['user_database']
|
| 150 |
+
if username in user_db and user_db[username]['password'] == hashed_password:
|
| 151 |
+
token = jwt.encode({'user': username}, 'secret', algorithm='HS256')
|
| 152 |
+
st.session_state['auth_token'] = token
|
| 153 |
+
st.success("Logged in successfully!")
|
| 154 |
+
else:
|
| 155 |
+
st.error("Invalid username or password")
|
| 156 |
+
|
| 157 |
+
elif auth_option == "Create Account":
|
| 158 |
+
username = st.sidebar.text_input("Choose a Username")
|
| 159 |
+
password = st.sidebar.text_input("Choose a Password", type="password")
|
| 160 |
+
confirm_password = st.sidebar.text_input("Confirm Password", type="password")
|
| 161 |
+
if st.sidebar.button("Create Account"):
|
| 162 |
+
if password != confirm_password:
|
| 163 |
+
st.error("Passwords do not match")
|
| 164 |
+
elif username in st.session_state['user_database']:
|
| 165 |
+
st.error("Username already exists")
|
| 166 |
+
else:
|
| 167 |
+
# Hash the password
|
| 168 |
+
hashed_password = hashlib.sha256(password.encode()).hexdigest()
|
| 169 |
+
# Store user in the database
|
| 170 |
+
st.session_state['user_database'][username] = {'password': hashed_password}
|
| 171 |
+
st.success("Account created successfully! Please login.")
|
| 172 |
|
| 173 |
if 'auth_token' not in st.session_state:
|
| 174 |
authenticate_user()
|
| 175 |
else:
|
| 176 |
# Proceed with the application
|
| 177 |
|
| 178 |
+
# Decode JWT to get username
|
| 179 |
+
token = st.session_state['auth_token']
|
| 180 |
+
decoded_token = jwt.decode(token, 'secret', algorithms=['HS256'])
|
| 181 |
+
username = decoded_token['user']
|
| 182 |
+
|
| 183 |
# Title of the app
|
| 184 |
+
st.title(f"Quantum Bank: Welcome, {username}")
|
| 185 |
st.sidebar.header("Navigation")
|
| 186 |
|
| 187 |
+
# Logout button
|
| 188 |
+
if st.sidebar.button("Logout"):
|
| 189 |
+
del st.session_state['auth_token']
|
| 190 |
+
st.success("Logged out successfully")
|
| 191 |
+
st.experimental_rerun()
|
| 192 |
+
|
| 193 |
# Main application navigation
|
| 194 |
service_choice = st.sidebar.selectbox("Choose a Service", [
|
| 195 |
"Dashboard",
|