11import {
22 initializeApp as initializeAdminApp ,
33 cert ,
4- getApp ,
5- getApps ,
4+ getApps as getAdminApps ,
65 applicationDefault ,
76 // renamed because there seems to be a global Credential type in vscode
87 Credential as FirebaseAdminCredential ,
98 AppOptions ,
109} from 'firebase-admin/app'
1110import { log } from './logging'
1211
12+ const FIREBASE_ADMIN_APP_NAME = 'vuefire-admin'
13+
1314/**
1415 * Setups a Firebase Admin App
1516 *
1617 * @param firebaseAdminOptions - options to pass to the admin app
18+ * @param name - name of the app
1719 * @experimental this is experimental and may change in the future
1820 */
1921export function getAdminApp (
20- firebaseAdminOptions ?: Omit < AppOptions , 'credential' >
22+ firebaseAdminOptions ?: Omit < AppOptions , 'credential' > ,
23+ name = FIREBASE_ADMIN_APP_NAME
2124) {
2225 // only initialize the admin sdk once
23- if ( ! getApps ( ) . length ) {
26+ log ( 'debug' , `💭 Getting admin app "${ name } ` )
27+
28+ if ( ! getAdminApps ( ) . find ( ( app ) => app . name === name ) ) {
29+ log ( 'debug' , `🔶 Initializing admin app "${ name } "` )
2430 const {
2531 // these can be set by the user on other platforms
2632 FIREBASE_PROJECT_ID ,
@@ -45,7 +51,7 @@ export function getAdminApp(
4551 if ( FIREBASE_CONFIG || FUNCTION_NAME ) {
4652 // TODO: last time I tried this one fails on the server
4753 log ( 'debug' , `using FIREBASE_CONFIG env variable for ${ FUNCTION_NAME } ` )
48- initializeAdminApp ( )
54+ initializeAdminApp ( undefined , name )
4955 } else {
5056 let credential : FirebaseAdminCredential
5157
@@ -102,14 +108,17 @@ export function getAdminApp(
102108 // )
103109 // throw new Error('admin-app/missing-credentials')
104110
105- initializeAdminApp ( {
106- // TODO: is this really going to be used?
107- ...firebaseAdminOptions ,
108- credential,
109- } )
111+ initializeAdminApp (
112+ {
113+ // TODO: is this really going to be used?
114+ ...firebaseAdminOptions ,
115+ credential,
116+ } ,
117+ name
118+ )
110119 }
111120 }
112121
113122 // we know have a valid admin app
114- return getApp ( ) !
123+ return getAdminApps ( ) . find ( ( app ) => app . name === name ) !
115124}
0 commit comments