-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebase.ts
More file actions
34 lines (25 loc) · 767 Bytes
/
firebase.ts
File metadata and controls
34 lines (25 loc) · 767 Bytes
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
import { App, cert, getApps, initializeApp } from 'firebase-admin/app'
import { getMessaging } from 'firebase-admin/messaging'
function getServiceAccount() {
const rawServiceAccount = process.env.GOOGLE_SERVICES_JSON
if (!rawServiceAccount) {
return null
}
return JSON.parse(rawServiceAccount)
}
export function getFirebaseApp(): App {
const existingApp = getApps()[0]
if (existingApp) {
return existingApp
}
const serviceAccount = getServiceAccount()
if (!serviceAccount) {
throw new Error('Firebase Admin is not configured. Set GOOGLE_SERVICES_JSON before using FCM.')
}
return initializeApp({
credential: cert(serviceAccount),
})
}
export function getFirebaseMessaging() {
return getMessaging(getFirebaseApp())
}