@@ -14,16 +14,31 @@ export default defineNuxtPlugin((nuxtApp) => {
14
14
15
15
// only initialize the admin sdk once
16
16
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
+ )
26
27
}
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
+ } )
27
42
}
28
43
29
44
const firebaseAdminApp = getApp ( )
0 commit comments