Wire firebase-admin with ADC or FIREBASE_SERVICE_ACCOUNT_JSON, export messaging from src/services/firebase.ts, and load it at server startup.
20 lines
598 B
TypeScript
20 lines
598 B
TypeScript
import admin from "firebase-admin";
|
|
import type { ServiceAccount } from "firebase-admin/app";
|
|
import type { Messaging } from "firebase-admin/messaging";
|
|
|
|
function resolveCredential(): admin.credential.Credential {
|
|
const json = process.env.FIREBASE_SERVICE_ACCOUNT_JSON;
|
|
if (json !== undefined && json.trim() !== "") {
|
|
return admin.credential.cert(JSON.parse(json) as ServiceAccount);
|
|
}
|
|
return admin.credential.applicationDefault();
|
|
}
|
|
|
|
if (!admin.apps.length) {
|
|
admin.initializeApp({
|
|
credential: resolveCredential(),
|
|
});
|
|
}
|
|
|
|
export const messaging: Messaging = admin.messaging();
|