vasuzex 2.1.15 → 2.1.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
* Firebase Admin SDK Singleton
|
|
3
3
|
* Centralized Firebase Admin initialization for FCM
|
|
4
4
|
*
|
|
5
|
-
* Configuration
|
|
6
|
-
* -
|
|
7
|
-
*
|
|
8
|
-
* - FIREBASE_PRIVATE_KEY
|
|
5
|
+
* Configuration options (in priority order):
|
|
6
|
+
* 1. FIREBASE_SERVICE_ACCOUNT_PATH - path to service account JSON file
|
|
7
|
+
* 2. Environment variables: FIREBASE_PROJECT_ID, FIREBASE_CLIENT_EMAIL, FIREBASE_PRIVATE_KEY
|
|
9
8
|
*/
|
|
10
9
|
|
|
11
10
|
import admin from 'firebase-admin';
|
|
11
|
+
import { readFileSync } from 'fs';
|
|
12
|
+
import { resolve } from 'path';
|
|
12
13
|
import { Config, Log } from '../../Support/Facades/index.js';
|
|
13
14
|
|
|
14
15
|
let firebaseApp = null;
|
|
@@ -24,20 +25,41 @@ export function initializeFirebaseAdmin() {
|
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
try {
|
|
28
|
+
// Option 1: Load from service account file (preferred)
|
|
29
|
+
const serviceAccountPath = Config.get('notification.fcm.service_account_path') || process.env.FIREBASE_SERVICE_ACCOUNT_PATH;
|
|
30
|
+
|
|
31
|
+
if (serviceAccountPath) {
|
|
32
|
+
// Use PROJECT_ROOT if available, otherwise use process.cwd()
|
|
33
|
+
const projectRoot = process.env.PROJECT_ROOT || process.cwd();
|
|
34
|
+
|
|
35
|
+
// Resolve from project root if relative, use as-is if absolute
|
|
36
|
+
const fullPath = serviceAccountPath.startsWith('/')
|
|
37
|
+
? serviceAccountPath
|
|
38
|
+
: resolve(projectRoot, serviceAccountPath);
|
|
39
|
+
const serviceAccount = JSON.parse(readFileSync(fullPath, 'utf8'));
|
|
40
|
+
|
|
41
|
+
firebaseApp = admin.initializeApp({
|
|
42
|
+
credential: admin.credential.cert(serviceAccount)
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
isInitialized = true;
|
|
46
|
+
Log.info('Firebase Admin SDK initialized from service account file', {
|
|
47
|
+
projectId: serviceAccount.project_id,
|
|
48
|
+
path: fullPath
|
|
49
|
+
});
|
|
50
|
+
return firebaseApp;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Option 2: Load from environment variables
|
|
27
54
|
const projectId = Config.get('notification.fcm.project_id') || process.env.FIREBASE_PROJECT_ID;
|
|
28
55
|
const clientEmail = Config.get('notification.fcm.client_email') || process.env.FIREBASE_CLIENT_EMAIL;
|
|
29
56
|
const privateKey = Config.get('notification.fcm.private_key') || process.env.FIREBASE_PRIVATE_KEY;
|
|
30
57
|
|
|
31
58
|
if (!projectId || !clientEmail || !privateKey) {
|
|
32
|
-
Log.warning('Firebase Admin SDK not configured - FCM notifications disabled'
|
|
33
|
-
hasProjectId: !!projectId,
|
|
34
|
-
hasClientEmail: !!clientEmail,
|
|
35
|
-
hasPrivateKey: !!privateKey
|
|
36
|
-
});
|
|
59
|
+
Log.warning('Firebase Admin SDK not configured - FCM notifications disabled');
|
|
37
60
|
return null;
|
|
38
61
|
}
|
|
39
62
|
|
|
40
|
-
// Handle escaped newlines in private key
|
|
41
63
|
const formattedPrivateKey = privateKey.replace(/\\n/g, '\n');
|
|
42
64
|
|
|
43
65
|
firebaseApp = admin.initializeApp({
|
|
@@ -49,13 +71,11 @@ export function initializeFirebaseAdmin() {
|
|
|
49
71
|
});
|
|
50
72
|
|
|
51
73
|
isInitialized = true;
|
|
52
|
-
Log.info('Firebase Admin SDK initialized
|
|
53
|
-
|
|
74
|
+
Log.info('Firebase Admin SDK initialized from environment variables', { projectId });
|
|
54
75
|
return firebaseApp;
|
|
55
76
|
} catch (error) {
|
|
56
77
|
Log.error('Failed to initialize Firebase Admin SDK', {
|
|
57
|
-
error: error.message
|
|
58
|
-
stack: error.stack
|
|
78
|
+
error: error.message
|
|
59
79
|
});
|
|
60
80
|
return null;
|
|
61
81
|
}
|