-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebase_auth.py
More file actions
42 lines (38 loc) · 1.57 KB
/
firebase_auth.py
File metadata and controls
42 lines (38 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import streamlit as st
import firebase_admin
from firebase_admin import credentials, auth
import json
# ---------- Firebase Initialization ----------
def initialize_firebase():
if not firebase_admin._apps:
try:
# Convert Streamlit secret to a plain Python dict
firebase_creds_dict = json.loads(json.dumps(st.secrets["GCP"]))
cred = credentials.Certificate(firebase_creds_dict)
firebase_admin.initialize_app(cred)
st.success("✅ Firebase initialized successfully!")
except KeyError:
st.error("Firebase initialization failed: 'GCP' key not found in Streamlit secrets. Add your service account JSON under [GCP].")
except Exception as e:
st.error(f"Firebase initialization failed: {e}")
# Initialize on import
initialize_firebase()
# ---------- Create User ----------
def create_user(email: str, password: str):
try:
user = auth.create_user(email=email, password=password)
st.success(f"🎉 User created successfully! UID: {user.uid}")
return user.uid
except Exception as e:
st.error(f"Error creating user: {e}")
return None
# ---------- Sign In User ----------
# Note: Firebase Admin SDK cannot verify passwords directly
def sign_in_user(email: str, password: str):
try:
# Placeholder logic: simulate sign-in
st.info(f"Sign-in simulated successfully for {email} (Firebase Admin SDK limitation).")
return f"mock_uid_for_{email}"
except Exception as e:
st.error(f"Error signing in: {e}")
return None