|
| 1 | +import { Provider } from '@nestjs/common'; |
| 2 | +import admin from 'firebase-admin'; |
| 3 | +import fs from 'fs'; |
| 4 | +import { ConfigService } from '../common/configs/config.service'; |
| 5 | + |
| 6 | +export const FIREBASE_ADMIN = 'FIREBASE_ADMIN'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Firebase Admin provider. |
| 10 | + * |
| 11 | + * Initialization strategy: |
| 12 | + * 1. Prefer FIREBASE_SERVICE_ACCOUNT_KEY (stringified JSON) from env or ConfigService |
| 13 | + * 2. Fallback to FIREBASE_SERVICE_ACCOUNT_PATH (path to JSON file) |
| 14 | + * 3. If neither present, do not initialize and return the admin namespace (attempts to use it will fail with a clear log) |
| 15 | + */ |
| 16 | +export const FirebaseProvider: Provider = { |
| 17 | + provide: FIREBASE_ADMIN, |
| 18 | + useFactory: (configService: ConfigService) => { |
| 19 | + // Prefer process.env values. If not present, attempt to read from ConfigService.getConfigs() |
| 20 | + let envKey: string | undefined = process.env.FIREBASE_SERVICE_ACCOUNT_KEY; |
| 21 | + let envPath: string | undefined = process.env.FIREBASE_SERVICE_ACCOUNT_PATH; |
| 22 | + |
| 23 | + if ((!envKey || !envPath) && configService) { |
| 24 | + const cfg = (configService as any).getConfigs?.() as Record<string, any> | undefined; |
| 25 | + if (cfg) { |
| 26 | + if (!envKey && typeof cfg.FIREBASE_SERVICE_ACCOUNT_KEY === 'string') { |
| 27 | + envKey = cfg.FIREBASE_SERVICE_ACCOUNT_KEY; |
| 28 | + } |
| 29 | + if (!envPath && typeof cfg.FIREBASE_SERVICE_ACCOUNT_PATH === 'string') { |
| 30 | + envPath = cfg.FIREBASE_SERVICE_ACCOUNT_PATH; |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + if (!admin.apps.length) { |
| 36 | + let credentialObj: any = null; |
| 37 | + |
| 38 | + if (envKey) { |
| 39 | + try { |
| 40 | + credentialObj = JSON.parse(envKey); |
| 41 | + } catch (err) { |
| 42 | + console.error('[FirebaseProvider] FIREBASE_SERVICE_ACCOUNT_KEY is not valid JSON', err); |
| 43 | + } |
| 44 | + } else if (envPath) { |
| 45 | + try { |
| 46 | + const file = fs.readFileSync(String(envPath), 'utf8'); |
| 47 | + credentialObj = JSON.parse(file); |
| 48 | + } catch (err) { |
| 49 | + console.error('[FirebaseProvider] Failed to read/parse service account at path:', String(envPath), err); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if (credentialObj) { |
| 54 | + try { |
| 55 | + admin.initializeApp({ |
| 56 | + credential: admin.credential.cert(credentialObj), |
| 57 | + }); |
| 58 | + console.log('[FirebaseProvider] Initialized Firebase Admin SDK'); |
| 59 | + } catch (err) { |
| 60 | + console.error('[FirebaseProvider] Failed to initialize Firebase Admin:', err); |
| 61 | + } |
| 62 | + } else { |
| 63 | + console.warn( |
| 64 | + '[FirebaseProvider] No Firebase service account provided (FIREBASE_SERVICE_ACCOUNT_KEY or FIREBASE_SERVICE_ACCOUNT_PATH). Firebase Admin not initialized. Calls to firebase services will fail.' |
| 65 | + ); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return admin; |
| 70 | + }, |
| 71 | + inject: [ConfigService], |
| 72 | +}; |
0 commit comments