Skip to content

Commit c9461ef

Browse files
committed
fix: handle more service account cases
1 parent 42b35bf commit c9461ef

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

packages/nuxt/src/module.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@ const VueFire: NuxtModule<VueFireNuxtModuleOptions> =
114114
})
115115
}
116116

117+
// This one is set by servers, we set the GOOGLE_APPLICATION_CREDENTIALS env variable instead that has a lower priority and can be both a path or a JSON string
118+
// process.env.FIREBASE_CONFIG ||= JSON.stringify(options.config)
119+
if (typeof options.admin?.serviceAccount === 'string') {
120+
process.env.GOOGLE_APPLICATION_CREDENTIALS ||=
121+
options.admin.serviceAccount
122+
}
123+
const hasServiceAccount =
124+
typeof process.env.GOOGLE_APPLICATION_CREDENTIALS === 'string' &&
125+
process.env.GOOGLE_APPLICATION_CREDENTIALS.length > 0
126+
117127
// NOTE: the order of the plugins is reversed, so we end by adding the app plugin which is used by all other
118128
// plugins
119129

@@ -164,14 +174,7 @@ const VueFire: NuxtModule<VueFireNuxtModuleOptions> =
164174
'The "admin" option is only used during SSR. You should reenable ssr to use it.'
165175
)
166176
}
167-
// TODO: check env variables are present
168177

169-
// This one is set by servers, we set the GOOGLE_APPLICATION_CREDENTIALS env variable instead that has a lower priority and can be both a path or a JSON string
170-
// process.env.FIREBASE_CONFIG ||= JSON.stringify(options.config)
171-
if (typeof options.admin?.serviceAccount === 'string') {
172-
process.env.GOOGLE_APPLICATION_CREDENTIALS ||=
173-
options.admin.serviceAccount
174-
}
175178
// TODO: remove this runtime config if it's not needed as it could include sensitive data
176179
if (options.admin) {
177180
nuxt.options.appConfig.firebaseAdmin = markRaw(options.admin)
@@ -183,6 +186,8 @@ const VueFire: NuxtModule<VueFireNuxtModuleOptions> =
183186
addPlugin(resolve(runtimeDir, 'admin/plugin.server'))
184187
}
185188

189+
// Add auto imports that are useful to be auto imported
190+
186191
// these imports are overridden by nuxt-vuefire to allow being used in more places like plugins and middlewares
187192
addImports([
188193
// app

packages/nuxt/src/runtime/admin/plugin.server.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export default defineNuxtPlugin((nuxtApp) => {
1515

1616
const { firebaseAdmin } = appConfig
1717

18+
if (typeof firebaseAdmin?.serviceAccount === 'string') {
19+
process.env.GOOGLE_APPLICATION_CREDENTIALS ||= firebaseAdmin.serviceAccount
20+
}
21+
1822
// only initialize the admin sdk once
1923
if (!getApps().length) {
2024
const {
@@ -43,9 +47,14 @@ export default defineNuxtPlugin((nuxtApp) => {
4347
privateKey: FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
4448
})
4549
} else if (process.env.GOOGLE_APPLICATION_CREDENTIALS) {
46-
log('using GOOGLE_APPLICATION_CREDENTIALS env variable')
50+
log('debug', 'using GOOGLE_APPLICATION_CREDENTIALS env variable')
4751
// automatically picks up the service account file path from the env variable
4852
credential = applicationDefault()
53+
} else if (
54+
typeof firebaseAdmin?.serviceAccount === 'object' &&
55+
firebaseAdmin.serviceAccount != null
56+
) {
57+
credential = cert(firebaseAdmin.serviceAccount)
4958
} else {
5059
// TODO: add link to vuefire docs
5160
log(

0 commit comments

Comments
 (0)