Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,35 +13,41 @@ if firebase_creds:
|
|
13 |
else:
|
14 |
st.error("Firebase credentials not found. Please check your secrets.")
|
15 |
|
16 |
-
# Initialize
|
17 |
-
if not
|
18 |
-
|
19 |
-
|
20 |
-
st.
|
21 |
-
else:
|
22 |
-
st.error("Failed to initialize Firebase.")
|
23 |
|
24 |
-
#
|
25 |
-
def
|
|
|
|
|
26 |
try:
|
27 |
-
#
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
msg["Subject"] = "Verify Your Email"
|
33 |
-
msg["From"] = "[email protected]"
|
34 |
-
msg["To"] = email
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
43 |
except Exception as e:
|
44 |
-
st.error(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
# Registration form
|
47 |
def registration_form():
|
@@ -49,18 +55,35 @@ def registration_form():
|
|
49 |
st.subheader("Register")
|
50 |
email = st.text_input("Email", key="reg_email")
|
51 |
password = st.text_input("Password (min 6 characters)", type="password", key="reg_password")
|
52 |
-
submit_button = st.form_submit_button("Register")
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
|
65 |
-
#
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
else:
|
14 |
st.error("Firebase credentials not found. Please check your secrets.")
|
15 |
|
16 |
+
# Initialize session state
|
17 |
+
if "logged_in" not in st.session_state:
|
18 |
+
st.session_state.logged_in = False
|
19 |
+
if "current_user" not in st.session_state:
|
20 |
+
st.session_state.current_user = None
|
|
|
|
|
21 |
|
22 |
+
# Callback for registration
|
23 |
+
def register_callback():
|
24 |
+
email = st.session_state.reg_email
|
25 |
+
password = st.session_state.reg_password
|
26 |
try:
|
27 |
+
# Create a new user in Firebase
|
28 |
+
user = auth.create_user(email=email, password=password)
|
29 |
+
st.success("Registration successful! Please log in.")
|
30 |
+
except Exception as e:
|
31 |
+
st.error(f"Registration failed: {e}")
|
|
|
|
|
|
|
32 |
|
33 |
+
# Callback for login
|
34 |
+
def login_callback():
|
35 |
+
email = st.session_state.login_email
|
36 |
+
password = st.session_state.login_password
|
37 |
+
try:
|
38 |
+
# Authenticate user with Firebase
|
39 |
+
user = auth.get_user_by_email(email)
|
40 |
+
st.session_state.logged_in = True
|
41 |
+
st.session_state.current_user = user.uid # Store the user UID in session state
|
42 |
+
st.success("Logged in successfully!")
|
43 |
except Exception as e:
|
44 |
+
st.error(f"Login failed: {e}")
|
45 |
+
|
46 |
+
# Callback for logout
|
47 |
+
def logout_callback():
|
48 |
+
st.session_state.logged_in = False
|
49 |
+
st.session_state.current_user = None
|
50 |
+
st.info("Logged out successfully!")
|
51 |
|
52 |
# Registration form
|
53 |
def registration_form():
|
|
|
55 |
st.subheader("Register")
|
56 |
email = st.text_input("Email", key="reg_email")
|
57 |
password = st.text_input("Password (min 6 characters)", type="password", key="reg_password")
|
58 |
+
submit_button = st.form_submit_button("Register", on_click=register_callback)
|
59 |
|
60 |
+
# Login form
|
61 |
+
def login_form():
|
62 |
+
with st.form("Login"):
|
63 |
+
st.subheader("Login")
|
64 |
+
email = st.text_input("Email", key="login_email")
|
65 |
+
password = st.text_input("Password", type="password", key="login_password")
|
66 |
+
submit_button = st.form_submit_button("Login", on_click=login_callback)
|
67 |
|
68 |
+
# Main app screen (after login)
|
69 |
+
def main_app():
|
70 |
+
st.subheader(f"Welcome, {st.session_state.current_user}!")
|
71 |
+
st.write("Enter a name below to get a greeting.")
|
72 |
|
73 |
+
# Input field for name
|
74 |
+
name = st.text_input("Enter a name", key="name_input")
|
75 |
+
|
76 |
+
# Display greeting in real-time
|
77 |
+
if name:
|
78 |
+
st.write(f"Hello {name}!")
|
79 |
+
|
80 |
+
# Logout button
|
81 |
+
if st.button("Logout", on_click=logout_callback):
|
82 |
+
pass
|
83 |
+
|
84 |
+
# Render the appropriate screen based on login status
|
85 |
+
if st.session_state.logged_in:
|
86 |
+
main_app()
|
87 |
+
else:
|
88 |
+
registration_form()
|
89 |
+
login_form()
|