Spaces:
Sleeping
Sleeping
File size: 2,322 Bytes
feeaf83 | 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | import json
import os
import re
def setup_firebase():
json_path = "service-account.json"
env_path = ".env"
if not os.path.exists(json_path):
print(f"β Error: {json_path} not found in the current directory.")
print("Please download your service account JSON from Firebase Console and save it as 'service-account.json'.")
return
try:
with open(json_path, 'r') as f:
data = json.load(f)
print("β
service-account.json loaded successfully.")
# Sanitize Private Key for .env
pk = data.get("private_key", "")
# We want to store it in a way that our initialized_firebase logic can handle
# Option A: Store the raw block with literal \n (standard)
pk_env = pk.replace("\n", "\\n")
updates = {
"FIREBASE_PROJECT_ID": data.get("project_id"),
"FIREBASE_PRIVATE_KEY_ID": data.get("private_key_id"),
"FIREBASE_PRIVATE_KEY": pk_env,
"FIREBASE_CLIENT_EMAIL": data.get("client_email"),
"FIREBASE_CLIENT_ID": data.get("client_id"),
"FIREBASE_CLIENT_CERT_URL": data.get("client_x509_cert_url")
}
# Read existing .env
env_lines = []
if os.path.exists(env_path):
with open(env_path, 'r') as f:
env_lines = f.readlines()
# Update or Add keys
new_lines = []
keys_handled = set()
for line in env_lines:
match = re.match(r'^([^=]+)=(.*)$', line)
if match:
k, v = match.groups()
if k in updates:
new_lines.append(f"{k}=\"{updates[k]}\"\n")
keys_handled.add(k)
continue
new_lines.append(line)
for k, v in updates.items():
if k not in keys_handled:
new_lines.append(f"{k}=\"{v}\"\n")
with open(env_path, 'w') as f:
f.writelines(new_lines)
print(f"β
{env_path} updated with Firebase credentials.")
print("\nπ You can now restart your server.")
except Exception as e:
print(f"β Error during setup: {e}")
if __name__ == "__main__":
setup_firebase()
|