ShadowGard3n commited on
Commit
0f237c9
·
1 Parent(s): f639e70

Changes service account key format

Browse files
Files changed (1) hide show
  1. service/firebase_service.py +21 -4
service/firebase_service.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  import firebase_admin
3
  from firebase_admin import auth, credentials, firestore
4
  import requests
 
5
  from dotenv import load_dotenv
6
  from fastapi import HTTPException
7
  from fastapi import Request
@@ -14,11 +15,27 @@ import requests
14
  load_dotenv()
15
 
16
  if not firebase_admin._apps:
17
- key_path = os.getenv("SERVICE_ACCOUNT_KEY_PATH")
 
18
 
19
- if not key_path:
20
- raise ValueError("SERVICE_ACCOUNT_KEY_PATH environment variable not set.")
21
- cred = credentials.Certificate(key_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  firebase_admin.initialize_app(cred)
23
 
24
  db = firestore.client()
 
2
  import firebase_admin
3
  from firebase_admin import auth, credentials, firestore
4
  import requests
5
+ import json
6
  from dotenv import load_dotenv
7
  from fastapi import HTTPException
8
  from fastapi import Request
 
15
  load_dotenv()
16
 
17
  if not firebase_admin._apps:
18
+ # 1. Try to get the credentials from the full JSON string (Best for Deployment)
19
+ firebase_creds_json = os.getenv("FIREBASE_CREDENTIALS_JSON")
20
 
21
+ if firebase_creds_json:
22
+ # Parse the JSON string directly
23
+ cred_dict = json.loads(firebase_creds_json)
24
+ cred = credentials.Certificate(cred_dict)
25
+
26
+ else:
27
+ # 2. Fallback: Try to load from a file path (Best for Local Development)
28
+ key_path = os.getenv("SERVICE_ACCOUNT_KEY_PATH", "serviceAccountKey.json")
29
+
30
+ if not os.path.exists(key_path):
31
+ # If neither exists, we can't connect
32
+ raise ValueError(
33
+ "No Firebase credentials found! Set 'FIREBASE_CREDENTIALS_JSON' (deployment) "
34
+ "or ensure 'serviceAccountKey.json' exists locally."
35
+ )
36
+
37
+ cred = credentials.Certificate(key_path)
38
+
39
  firebase_admin.initialize_app(cred)
40
 
41
  db = firestore.client()