Spaces:
Sleeping
Sleeping
fix: pass projectId explicitly to Firebase init to fix Google/GitHub sign-in
Browse files- backend/app.py +41 -9
backend/app.py
CHANGED
|
@@ -23,26 +23,49 @@ try:
|
|
| 23 |
# Priority 2: Local credentials file
|
| 24 |
cred_path = os.path.join(os.path.dirname(__file__), 'firebase-credentials.json')
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
if firebase_creds_json:
|
| 27 |
cred_dict = json.loads(firebase_creds_json)
|
| 28 |
cred = firebase_admin.credentials.Certificate(cred_dict)
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
elif os.path.exists(cred_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
cred = firebase_admin.credentials.Certificate(cred_path)
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
else:
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
if firebase_project_id and firebase_project_id != 'your-firebase-project-id':
|
|
|
|
| 38 |
firebase_admin.initialize_app(options={'projectId': firebase_project_id})
|
| 39 |
-
print(f"[Firebase] Admin SDK initialized for project
|
|
|
|
| 40 |
else:
|
| 41 |
firebase_admin.initialize_app()
|
| 42 |
-
print("[Firebase] Admin SDK initialized
|
| 43 |
firebase_initialized = True
|
| 44 |
except Exception as e:
|
| 45 |
-
print(f"[Firebase]
|
|
|
|
|
|
|
| 46 |
|
| 47 |
from datetime import datetime, timedelta
|
| 48 |
from functools import wraps
|
|
@@ -248,7 +271,12 @@ def google_login():
|
|
| 248 |
|
| 249 |
try:
|
| 250 |
# Verify the ID token using Firebase Admin SDK
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
decoded_token = firebase_auth.verify_id_token(id_token)
|
|
|
|
| 252 |
email = decoded_token.get('email')
|
| 253 |
if email:
|
| 254 |
email = email.lower()
|
|
@@ -298,7 +326,11 @@ def google_login():
|
|
| 298 |
}), 201 if is_new_user else 200
|
| 299 |
|
| 300 |
except Exception as e:
|
| 301 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
return jsonify({'error': f"Failed to verify ID token: {str(e)}"}), 401
|
| 303 |
|
| 304 |
@app.route('/api/auth/logout', methods=['POST'])
|
|
|
|
| 23 |
# Priority 2: Local credentials file
|
| 24 |
cred_path = os.path.join(os.path.dirname(__file__), 'firebase-credentials.json')
|
| 25 |
|
| 26 |
+
# Debug: log which paths are available
|
| 27 |
+
print(f"[Firebase] FIREBASE_CREDENTIALS env var set: {bool(firebase_creds_json)} (length: {len(firebase_creds_json)})")
|
| 28 |
+
print(f"[Firebase] Credentials file exists at {cred_path}: {os.path.exists(cred_path)}")
|
| 29 |
+
|
| 30 |
if firebase_creds_json:
|
| 31 |
cred_dict = json.loads(firebase_creds_json)
|
| 32 |
cred = firebase_admin.credentials.Certificate(cred_dict)
|
| 33 |
+
project_id = cred_dict.get('project_id') or os.environ.get('FIREBASE_PROJECT_ID') or os.environ.get('GOOGLE_CLOUD_PROJECT')
|
| 34 |
+
# Always set GOOGLE_CLOUD_PROJECT so the auth service can find it
|
| 35 |
+
if project_id:
|
| 36 |
+
os.environ.setdefault('GOOGLE_CLOUD_PROJECT', project_id)
|
| 37 |
+
firebase_admin.initialize_app(cred, options={'projectId': project_id} if project_id else None)
|
| 38 |
+
print(f"[Firebase] Admin SDK initialized from FIREBASE_CREDENTIALS env var (project: {project_id or '?'}).")
|
| 39 |
elif os.path.exists(cred_path):
|
| 40 |
+
# Read the file to extract project_id for explicit options
|
| 41 |
+
with open(cred_path, 'r') as f:
|
| 42 |
+
cred_file_data = json.load(f)
|
| 43 |
+
project_id = cred_file_data.get('project_id') or os.environ.get('FIREBASE_PROJECT_ID') or os.environ.get('GOOGLE_CLOUD_PROJECT')
|
| 44 |
cred = firebase_admin.credentials.Certificate(cred_path)
|
| 45 |
+
# Always set GOOGLE_CLOUD_PROJECT so the auth service can find it
|
| 46 |
+
if project_id:
|
| 47 |
+
os.environ.setdefault('GOOGLE_CLOUD_PROJECT', project_id)
|
| 48 |
+
firebase_admin.initialize_app(cred, options={'projectId': project_id} if project_id else None)
|
| 49 |
+
print(f"[Firebase] Admin SDK initialized using service account credentials file (project: {project_id or '?'}).")
|
| 50 |
else:
|
| 51 |
+
# Fallback: try FIREBASE_PROJECT_ID or GOOGLE_CLOUD_PROJECT
|
| 52 |
+
firebase_project_id = (
|
| 53 |
+
os.environ.get('FIREBASE_PROJECT_ID')
|
| 54 |
+
or os.environ.get('GOOGLE_CLOUD_PROJECT')
|
| 55 |
+
)
|
| 56 |
if firebase_project_id and firebase_project_id != 'your-firebase-project-id':
|
| 57 |
+
os.environ.setdefault('GOOGLE_CLOUD_PROJECT', firebase_project_id)
|
| 58 |
firebase_admin.initialize_app(options={'projectId': firebase_project_id})
|
| 59 |
+
print(f"[Firebase] WARNING: Admin SDK initialized for project '{firebase_project_id}' WITHOUT service account credentials.")
|
| 60 |
+
print(f"[Firebase] WARNING: Token verification WILL FAIL. Set the FIREBASE_CREDENTIALS secret on HF Spaces.")
|
| 61 |
else:
|
| 62 |
firebase_admin.initialize_app()
|
| 63 |
+
print("[Firebase] WARNING: Admin SDK initialized with NO project ID and NO credentials. OAuth login will NOT work.")
|
| 64 |
firebase_initialized = True
|
| 65 |
except Exception as e:
|
| 66 |
+
print(f"[Firebase] ERROR: Could not initialize Firebase Admin: {e}")
|
| 67 |
+
import traceback
|
| 68 |
+
traceback.print_exc()
|
| 69 |
|
| 70 |
from datetime import datetime, timedelta
|
| 71 |
from functools import wraps
|
|
|
|
| 271 |
|
| 272 |
try:
|
| 273 |
# Verify the ID token using Firebase Admin SDK
|
| 274 |
+
import sys
|
| 275 |
+
print(f"[Auth] Verifying ID token (length: {len(id_token)})...", flush=True)
|
| 276 |
+
print(f"[Auth] Firebase app name: {firebase_admin.get_app().name}", flush=True)
|
| 277 |
+
print(f"[Auth] Firebase app project_id: {firebase_admin.get_app().project_id}", flush=True)
|
| 278 |
decoded_token = firebase_auth.verify_id_token(id_token)
|
| 279 |
+
print(f"[Auth] Token verified successfully. Provider: {decoded_token.get('firebase', {}).get('sign_in_provider', 'unknown')}", flush=True)
|
| 280 |
email = decoded_token.get('email')
|
| 281 |
if email:
|
| 282 |
email = email.lower()
|
|
|
|
| 326 |
}), 201 if is_new_user else 200
|
| 327 |
|
| 328 |
except Exception as e:
|
| 329 |
+
import sys, traceback
|
| 330 |
+
print(f"[Auth] ERROR during authentication: {e}", flush=True)
|
| 331 |
+
traceback.print_exc()
|
| 332 |
+
sys.stdout.flush()
|
| 333 |
+
sys.stderr.flush()
|
| 334 |
return jsonify({'error': f"Failed to verify ID token: {str(e)}"}), 401
|
| 335 |
|
| 336 |
@app.route('/api/auth/logout', methods=['POST'])
|