Spaces:
Sleeping
Sleeping
import os | |
import json | |
import streamlit as st | |
import firebase_admin | |
from firebase_admin import credentials, auth | |
# Retrieve the JSON string from the secret | |
json_str = os.getenv("FIREBASE_CREDENTIALS") | |
# Retrieve the JSON string from the secret | |
json_str = os.getenv("FIREBASE_CREDENTIALS") | |
firebase_Creds = json.loads(json_str) | |
# Initialize Firebase (only once) | |
if not firebase_admin._apps: | |
cred = credentials.Certificate(firebase_Creds) # Replace with your Firebase service account key | |
firebase_admin.initialize_app(cred) | |
# Initialize session state | |
if "logged_in" not in st.session_state: | |
st.session_state.logged_in = False | |
if "current_user" not in st.session_state: | |
st.session_state.current_user = None | |
# Callback for registration | |
def register_callback(): | |
email = st.session_state.reg_email | |
password = st.session_state.reg_password | |
try: | |
# Create a new user in Firebase | |
user = auth.create_user_with_email_and_password(email, password) | |
st.success("Registration successful! Please check your email for verification.") | |
# Send verification email | |
auth.send_email_verification(user['idToken']) | |
st.info("A verification email has been sent to your email address.") | |
except Exception as e: | |
st.error(f"Registration failed: {e}") | |
# Callback for login | |
def login_callback(): | |
email = st.session_state.login_email | |
password = st.session_state.login_password | |
try: | |
# Authenticate user with Firebase | |
user = auth.sign_in_with_email_and_password(email, password) | |
# Check if email is verified | |
user_info = auth.get_account_info(user['idToken']) | |
if user_info['users'][0]['emailVerified']: | |
st.session_state.logged_in = True | |
st.session_state.current_user = user['localId'] | |
st.success("Logged in successfully!") | |
else: | |
st.error("Please verify your email address before logging in.") | |
# Resend verification email if needed | |
auth.send_email_verification(user['idToken']) | |
st.info("A new verification email has been sent to your email address.") | |
except Exception as e: | |
st.error(f"Login failed: {e}") | |
# Callback for logout | |
def logout_callback(): | |
st.session_state.logged_in = False | |
st.session_state.current_user = None | |
st.info("Logged out successfully!") | |
# Registration form | |
def registration_form(): | |
with st.form("Registration"): | |
st.subheader("Register") | |
email = st.text_input("Email", key="reg_email") | |
password = st.text_input("Password (min 6 characters)", type="password", key="reg_password") | |
submit_button = st.form_submit_button("Register", on_click=register_callback) | |
# Login form | |
def login_form(): | |
with st.form("Login"): | |
st.subheader("Login") | |
email = st.text_input("Email", key="login_email") | |
password = st.text_input("Password", type="password", key="login_password") | |
submit_button = st.form_submit_button("Login", on_click=login_callback) | |
# Main app screen (after login) | |
def main_app(): | |
st.subheader(f"Welcome, {st.session_state.current_user}!") | |
st.write("Enter a name below to get a greeting.") | |
# Input field for name | |
name = st.text_input("Enter a name", key="name_input") | |
# Display greeting in real-time | |
if name: | |
st.write(f"Hello {name}!") | |
# Logout button | |
if st.button("Logout", on_click=logout_callback): | |
pass | |
# Render the appropriate screen based on login status | |
if st.session_state.logged_in: | |
main_app() | |
else: | |
registration_form() | |
login_form() | |