Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,47 @@
|
|
1 |
-
import os
|
2 |
-
import json
|
3 |
import streamlit as st
|
4 |
import firebase_admin
|
5 |
from firebase_admin import credentials, auth
|
6 |
-
import
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
13 |
|
14 |
-
# Initialize
|
15 |
-
if
|
16 |
-
|
17 |
-
|
18 |
-
st.
|
|
|
|
|
19 |
|
20 |
-
#
|
21 |
-
def
|
22 |
-
email = st.session_state.reg_email
|
23 |
-
password = st.session_state.reg_password
|
24 |
try:
|
25 |
-
#
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
st.error(f"Registration failed: {e}")
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
# Authenticate user with Firebase
|
41 |
-
user = auth.sign_in_with_email_and_password(email, password)
|
42 |
|
43 |
-
|
44 |
-
user_info = auth.get_account_info(user['idToken'])
|
45 |
-
if user_info['users'][0]['emailVerified']:
|
46 |
-
st.session_state.logged_in = True
|
47 |
-
st.session_state.current_user = user['localId']
|
48 |
-
st.success("Logged in successfully!")
|
49 |
-
else:
|
50 |
-
st.error("Please verify your email address before logging in.")
|
51 |
-
# Resend verification email if needed
|
52 |
-
auth.send_email_verification(user['idToken'])
|
53 |
-
st.info("A new verification email has been sent to your email address.")
|
54 |
except Exception as e:
|
55 |
-
st.error(f"
|
56 |
-
|
57 |
-
# Callback for logout
|
58 |
-
def logout_callback():
|
59 |
-
st.session_state.logged_in = False
|
60 |
-
st.session_state.current_user = None
|
61 |
-
st.info("Logged out successfully!")
|
62 |
|
63 |
# Registration form
|
64 |
def registration_form():
|
@@ -66,35 +49,18 @@ def registration_form():
|
|
66 |
st.subheader("Register")
|
67 |
email = st.text_input("Email", key="reg_email")
|
68 |
password = st.text_input("Password (min 6 characters)", type="password", key="reg_password")
|
69 |
-
submit_button = st.form_submit_button("Register"
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
password = st.text_input("Password", type="password", key="login_password")
|
77 |
-
submit_button = st.form_submit_button("Login", on_click=login_callback)
|
78 |
|
79 |
-
#
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
# Display greeting in real-time
|
88 |
-
if name:
|
89 |
-
st.write(f"Hello {name}!")
|
90 |
-
|
91 |
-
# Logout button
|
92 |
-
if st.button("Logout", on_click=logout_callback):
|
93 |
-
pass
|
94 |
-
|
95 |
-
# Render the appropriate screen based on login status
|
96 |
-
if st.session_state.logged_in:
|
97 |
-
main_app()
|
98 |
-
else:
|
99 |
-
registration_form()
|
100 |
-
login_form()
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import firebase_admin
|
3 |
from firebase_admin import credentials, auth
|
4 |
+
import json
|
5 |
+
import os
|
6 |
+
import smtplib
|
7 |
+
from email.mime.text import MIMEText
|
8 |
|
9 |
+
# Load Firebase credentials from Hugging Face Secrets
|
10 |
+
firebase_creds = os.getenv("FIREBASE_CREDENTIALS")
|
11 |
+
if firebase_creds:
|
12 |
+
firebase_creds = json.loads(firebase_creds)
|
13 |
+
else:
|
14 |
+
st.error("Firebase credentials not found. Please check your secrets.")
|
15 |
|
16 |
+
# Initialize Firebase (only once)
|
17 |
+
if not firebase_admin._apps and firebase_creds:
|
18 |
+
cred = credentials.Certificate(firebase_creds)
|
19 |
+
firebase_admin.initialize_app(cred)
|
20 |
+
st.success("Firebase initialized successfully!")
|
21 |
+
else:
|
22 |
+
st.error("Failed to initialize Firebase.")
|
23 |
|
24 |
+
# Function to send verification email
|
25 |
+
def send_verification_email(email):
|
|
|
|
|
26 |
try:
|
27 |
+
# Generate email verification link
|
28 |
+
verification_link = auth.generate_email_verification_link(email)
|
29 |
+
|
30 |
+
# Send email using SMTP (replace with your email service)
|
31 |
+
msg = MIMEText(f"Please verify your email by clicking this link: {verification_link}")
|
32 |
+
msg["Subject"] = "Verify Your Email"
|
33 |
+
msg["From"] = "your-email@example.com"
|
34 |
+
msg["To"] = email
|
|
|
35 |
|
36 |
+
# Replace with your SMTP server details
|
37 |
+
with smtplib.SMTP("smtp.example.com", 587) as server:
|
38 |
+
server.starttls()
|
39 |
+
server.login("your-email@example.com", "your-email-password")
|
40 |
+
server.sendmail("[email protected]", [email], msg.as_string())
|
|
|
|
|
41 |
|
42 |
+
st.success("Verification email sent successfully!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
except Exception as e:
|
44 |
+
st.error(f"Failed to send verification email: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
# Registration form
|
47 |
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 |
+
if submit_button:
|
55 |
+
try:
|
56 |
+
# Create a new user in Firebase
|
57 |
+
user = auth.create_user(email=email, password=password)
|
58 |
+
st.success("Registration successful! Please check your email for verification.")
|
|
|
|
|
59 |
|
60 |
+
# Send verification email
|
61 |
+
send_verification_email(email)
|
62 |
+
except Exception as e:
|
63 |
+
st.error(f"Registration failed: {e}")
|
64 |
|
65 |
+
# Render the registration form
|
66 |
+
registration_form()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|