Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import json
|
|
| 6 |
|
| 7 |
# Load Firebase credentials from Hugging Face Secrets
|
| 8 |
firebase_creds = os.getenv("FIREBASE_CREDENTIALS")
|
| 9 |
-
|
| 10 |
if firebase_creds:
|
| 11 |
firebase_creds = json.loads(firebase_creds)
|
| 12 |
else:
|
|
@@ -25,14 +25,53 @@ if "logged_in" not in st.session_state:
|
|
| 25 |
if "current_user" not in st.session_state:
|
| 26 |
st.session_state.current_user = None
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
# Callback for registration
|
| 29 |
def register_callback():
|
| 30 |
email = st.session_state.reg_email
|
| 31 |
password = st.session_state.reg_password
|
| 32 |
try:
|
| 33 |
-
# Create a new user in Firebase
|
| 34 |
user = auth.create_user(email=email, password=password)
|
| 35 |
-
st.success("Registration successful!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
st.error(f"Registration failed: {e}")
|
| 38 |
|
|
|
|
| 6 |
|
| 7 |
# Load Firebase credentials from Hugging Face Secrets
|
| 8 |
firebase_creds = os.getenv("FIREBASE_CREDENTIALS")
|
| 9 |
+
FIREBASE_API_KEY = os.getenv("FIREBASE_API_KEY")
|
| 10 |
if firebase_creds:
|
| 11 |
firebase_creds = json.loads(firebase_creds)
|
| 12 |
else:
|
|
|
|
| 25 |
if "current_user" not in st.session_state:
|
| 26 |
st.session_state.current_user = None
|
| 27 |
|
| 28 |
+
def send_verification_email(id_token):
|
| 29 |
+
url = f'https://identitytoolkit.googleapis.com/v1/accounts:sendOobCode?key={FIREBASE_API_KEY}'
|
| 30 |
+
headers = {'Content-Type': 'application/json'}
|
| 31 |
+
data = {
|
| 32 |
+
'requestType': 'VERIFY_EMAIL',
|
| 33 |
+
'idToken': id_token
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
response = requests.post(url, headers=headers, json=data)
|
| 37 |
+
result = response.json()
|
| 38 |
+
|
| 39 |
+
if 'error' in result:
|
| 40 |
+
return {'status': 'error', 'message': result['error']['message']}
|
| 41 |
+
else:
|
| 42 |
+
return {'status': 'success', 'email': result['email']}
|
| 43 |
+
|
| 44 |
# Callback for registration
|
| 45 |
def register_callback():
|
| 46 |
email = st.session_state.reg_email
|
| 47 |
password = st.session_state.reg_password
|
| 48 |
try:
|
| 49 |
+
# Step 1: Create a new user in Firebase
|
| 50 |
user = auth.create_user(email=email, password=password)
|
| 51 |
+
st.success("Registration successful! Sending verification email...")
|
| 52 |
+
|
| 53 |
+
# Step 2: Sign in the user programmatically to get the id_token
|
| 54 |
+
url = f'https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key={FIREBASE_API_KEY}'
|
| 55 |
+
data = {
|
| 56 |
+
'email': email,
|
| 57 |
+
'password': password,
|
| 58 |
+
'returnSecureToken': True
|
| 59 |
+
}
|
| 60 |
+
response = requests.post(url, json=data)
|
| 61 |
+
result = response.json()
|
| 62 |
+
|
| 63 |
+
if 'idToken' in result:
|
| 64 |
+
id_token = result['idToken'] # Retrieve the id_token
|
| 65 |
+
st.session_state.id_token = id_token # Store the id_token in session state
|
| 66 |
+
|
| 67 |
+
# Step 3: Send the verification email
|
| 68 |
+
verification_result = send_verification_email(id_token)
|
| 69 |
+
if verification_result['status'] == 'success':
|
| 70 |
+
st.success(f"Verification email sent to {email}.")
|
| 71 |
+
else:
|
| 72 |
+
st.error(f"Failed to send verification email: {verification_result['message']}")
|
| 73 |
+
else:
|
| 74 |
+
st.error(f"Failed to retrieve id_token: {result['error']['message']}")
|
| 75 |
except Exception as e:
|
| 76 |
st.error(f"Registration failed: {e}")
|
| 77 |
|