Update main.py
Browse files
main.py
CHANGED
|
@@ -81,16 +81,69 @@ def verify_admin(auth_header):
|
|
| 81 |
return uid
|
| 82 |
|
| 83 |
# βββ Ensure dummy admin exists βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
try:
|
| 85 |
-
admin_user = auth.get_user_by_email(
|
|
|
|
| 86 |
except firebase_admin._auth_utils.UserNotFoundError:
|
| 87 |
admin_user = auth.create_user(
|
| 88 |
-
email=
|
| 89 |
email_verified=True,
|
| 90 |
-
password=
|
| 91 |
display_name="Admin"
|
| 92 |
)
|
|
|
|
|
|
|
|
|
|
| 93 |
auth.set_custom_user_claims(admin_user.uid, {"admin": True})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
# Decorator for credit deduction
|
| 96 |
def credit_required(cost=1):
|
|
|
|
| 81 |
return uid
|
| 82 |
|
| 83 |
# βββ Ensure dummy admin exists βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 84 |
+
import firebase_admin
|
| 85 |
+
from firebase_admin import credentials, auth, db # Ensure db is imported
|
| 86 |
+
import os
|
| 87 |
+
import json
|
| 88 |
+
from datetime import datetime
|
| 89 |
+
|
| 90 |
+
# --- Firebase Initialization (ensure this runs once) ---
|
| 91 |
+
# You already have this in your app, just showing it here for context
|
| 92 |
+
# credentials_json_string = os.environ.get("FIREBASE")
|
| 93 |
+
# if credentials_json_string:
|
| 94 |
+
# credentials_json = json.loads(credentials_json_string)
|
| 95 |
+
# cred = credentials.Certificate(credentials_json)
|
| 96 |
+
# firebase_admin.initialize_app(cred, {
|
| 97 |
+
# 'databaseURL': 'YOUR_FIREBASE_DB_URL', # Replace with your actual DB URL
|
| 98 |
+
# 'storageBucket': 'YOUR_FIREBASE_STORAGE_BUCKET' # Replace with your actual Storage Bucket
|
| 99 |
+
# })
|
| 100 |
+
# else:
|
| 101 |
+
# print("FIREBASE secret not set. Firebase Admin SDK not initialized.")
|
| 102 |
+
# --------------------------------------------------------
|
| 103 |
+
|
| 104 |
+
# βββ Ensure dummy admin exists and has DB entry βββββββββββββββββββββββββββββββββββ
|
| 105 |
+
admin_email = "rairorr@gmail.com"
|
| 106 |
+
admin_password = "math1034!" # REMINDER: Change this for production!
|
| 107 |
+
|
| 108 |
try:
|
| 109 |
+
admin_user = auth.get_user_by_email(admin_email)
|
| 110 |
+
print(f"Admin user '{admin_email}' already exists in Firebase Auth.")
|
| 111 |
except firebase_admin._auth_utils.UserNotFoundError:
|
| 112 |
admin_user = auth.create_user(
|
| 113 |
+
email=admin_email,
|
| 114 |
email_verified=True,
|
| 115 |
+
password=admin_password,
|
| 116 |
display_name="Admin"
|
| 117 |
)
|
| 118 |
+
print(f"Admin user '{admin_email}' created in Firebase Auth with UID: {admin_user.uid}")
|
| 119 |
+
|
| 120 |
+
# Set custom claim for Firebase ID Token (for future direct claim checks)
|
| 121 |
auth.set_custom_user_claims(admin_user.uid, {"admin": True})
|
| 122 |
+
print(f"Custom claim 'admin: True' set for UID: {admin_user.uid}")
|
| 123 |
+
|
| 124 |
+
# --- IMPORTANT: Create/Update entry in Realtime Database ---
|
| 125 |
+
user_ref = db.reference(f'users/{admin_user.uid}')
|
| 126 |
+
user_data = user_ref.get()
|
| 127 |
+
|
| 128 |
+
if not user_data:
|
| 129 |
+
# If no entry exists, create a new one
|
| 130 |
+
user_data_to_set = {
|
| 131 |
+
'email': admin_email,
|
| 132 |
+
'credits': 999999, # Give admin lots of credits or set to 0 if not needed
|
| 133 |
+
'is_admin': True, # Explicitly set is_admin to True in DB
|
| 134 |
+
'created_at': datetime.utcnow().isoformat(),
|
| 135 |
+
'suspended': False
|
| 136 |
+
}
|
| 137 |
+
user_ref.set(user_data_to_set)
|
| 138 |
+
print(f"Admin user data created in Realtime Database for UID: {admin_user.uid}")
|
| 139 |
+
elif not user_data.get('is_admin', False):
|
| 140 |
+
# If entry exists but is_admin is false, update it
|
| 141 |
+
user_ref.update({'is_admin': True})
|
| 142 |
+
print(f"Admin user data updated in Realtime Database (is_admin set to True) for UID: {admin_user.uid}")
|
| 143 |
+
else:
|
| 144 |
+
print(f"Admin user data already exists and is_admin is True in Realtime Database for UID: {admin_user.uid}")
|
| 145 |
+
|
| 146 |
+
print("Admin setup complete.")
|
| 147 |
|
| 148 |
# Decorator for credit deduction
|
| 149 |
def credit_required(cost=1):
|