Spaces:
Sleeping
Sleeping
Download src/streamlit_app.py from fuerr18/rr: direct link, hf CLI and curl.
- Browser
- Download file 2.47 kB
-
https://huggingface.co/spaces/fuerr18/rr/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/fuerr18/rr/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/fuerr18/rr/resolve/main/src/streamlit_app.py
2.47 kB
| import streamlit as st | |
| import firebase_admin | |
| from firebase_admin import credentials, firestore | |
| import json | |
| # 1. تهيئة Firebase باستخدام Secrets | |
| if not firebase_admin._apps: | |
| try: | |
| # تأكدي أن الاسم في Settings هو FIREBASE_KEYS | |
| if "FIREBASE_KEYS" in st.secrets: | |
| fb_keys = st.secrets["FIREBASE_KEYS"] | |
| # تحويل النص إلى قاموس إذا لزم الأمر | |
| cred_dict = json.loads(fb_keys) if isinstance(fb_keys, str) else fb_keys | |
| cred = credentials.Certificate(cred_dict) | |
| firebase_admin.initialize_app(cred) | |
| else: | |
| st.error("خطأ: لم يتم العثور على FIREBASE_KEYS في الإعدادات.") | |
| except Exception as e: | |
| st.error(f"حدث خطأ أثناء التفعيل: {e}") | |
| # 2. تعريف قاعدة البيانات | |
| try: | |
| db = firestore.client() | |
| except: | |
| st.error("تأكدي من تفعيل خدمة Firestore في Firebase.") | |
| # 3. تهيئة حالة الجلسة (حل مشكلة KeyError: 'user') | |
| if "user" not in st.session_state: | |
| st.session_state["user"] = None | |
| # --- الآن يبدأ كود الواجهة --- | |
| st.title("بوابة الموظف") | |
| if st.session_state["user"] is None: | |
| auth_mode = st.radio("اختر العملية", ["دخول", "إنشاء حساب جديد"]) | |
| u_email = st.text_input("البريد الإلكتروني") | |
| u_pass = st.text_input("كلمة المرور", type="password") | |
| if auth_mode == "إنشاء حساب جديد": | |
| if st.button("تأكيد التسجيل"): | |
| if u_email and u_pass: | |
| db.collection("employee_accounts").add({ | |
| "email": u_email, | |
| "password": u_pass | |
| }) | |
| st.success("تم إنشاء الحساب بنجاح، يمكنك الدخول الآن.") | |
| else: | |
| if st.button("دخول"): | |
| # التأكد من الحساب | |
| docs = db.collection("employee_accounts").where("email", "==", u_email).where("password", "==", u_pass).get() | |
| if len(docs) > 0: | |
| st.session_state["user"] = u_email | |
| st.rerun() | |
| else: | |
| st.error("خطأ في بيانات الدخول") | |
| else: | |
| st.success(f"مرحباً بك: {st.session_state['user']}") | |
| if st.button("تسجيل الخروج"): | |
| st.session_state["user"] = None | |
| st.rerun() |