@@ -14,16 +14,31 @@ export default defineNuxtPlugin((nuxtApp) => {
1414
1515 // only initialize the admin sdk once
1616 if ( ! getApps ( ) . length ) {
17- // this is specified when deployed on Firebase and automatically picks up the credentials from env variables
18- if ( process . env . GCLOUD_PROJECT ) {
19- initializeApp ( )
20- } else {
21- initializeApp ( {
22- // TODO: is this really going to be used?
23- ...firebaseAdmin . config ,
24- credential : cert ( firebaseAdmin . serviceAccount ) ,
25- } )
17+ const { FIREBASE_PROJECT_ID , FIREBASE_CLIENT_EMAIL , FIREBASE_PRIVATE_KEY } =
18+ process . env
19+ // we need either a serviceAccount or the env variables
20+ if (
21+ ! firebaseAdmin . serviceAccount &&
22+ ( ! FIREBASE_CLIENT_EMAIL || ! FIREBASE_PRIVATE_KEY || ! FIREBASE_PROJECT_ID )
23+ ) {
24+ throw new Error (
25+ '[VueFire]: You must provide a "serviceAccount" or set the FIREBASE_CLIENT_EMAIL, FIREBASE_PRIVATE_KEY and FIREBASE_PROJECT_ID env variables.'
26+ )
2627 }
28+ initializeApp ( {
29+ // TODO: is this really going to be used?
30+ ...firebaseAdmin . config ,
31+ credential : cert (
32+ firebaseAdmin . serviceAccount || {
33+ // This version should work in Firebase Functions and other providers while applicationDefault() only works on
34+ // Firebase Functions. All values must exists because of the check above.
35+ projectId : FIREBASE_PROJECT_ID ! ,
36+ clientEmail : FIREBASE_CLIENT_EMAIL ! ,
37+ // replace `\` and `n` character pairs w/ single `\n` character
38+ privateKey : FIREBASE_PRIVATE_KEY ! . replace ( / \\ n / g, '\n' ) ,
39+ }
40+ ) ,
41+ } )
2742 }
2843
2944 const firebaseAdminApp = getApp ( )
0 commit comments