From 9415c8b04d3ab5dfe73ad669a8b62a33b6b0c439 Mon Sep 17 00:00:00 2001 From: Luca Grifo Date: Thu, 17 Jul 2025 21:10:13 +0200 Subject: [PATCH 1/2] fix: (0 , __vite_ssr_import_0__.getApps) is not a function --- .../nuxt/src/runtime/admin/plugin.server.ts | 4 +- .../runtime/auth/api.session-verification.ts | 2 +- src/server/admin.ts | 42 +++++++++++++++---- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/packages/nuxt/src/runtime/admin/plugin.server.ts b/packages/nuxt/src/runtime/admin/plugin.server.ts index 3e6d4db1..3a929314 100644 --- a/packages/nuxt/src/runtime/admin/plugin.server.ts +++ b/packages/nuxt/src/runtime/admin/plugin.server.ts @@ -2,11 +2,11 @@ import { type App as AdminApp } from 'firebase-admin/app' import { ensureAdminApp } from 'vuefire/server' import { defineNuxtPlugin, useRequestEvent, useRuntimeConfig } from '#imports' -export default defineNuxtPlugin(() => { +export default defineNuxtPlugin(async () => { const event = useRequestEvent() const { vuefire } = useRuntimeConfig() - const firebaseAdminApp = ensureAdminApp(vuefire?.admin?.options) + const firebaseAdminApp = await ensureAdminApp(vuefire?.admin?.options) // TODO: Is this accessible within middlewares and api routes? or should we use a middleware to add it event.context.firebaseApp = firebaseAdminApp diff --git a/packages/nuxt/src/runtime/auth/api.session-verification.ts b/packages/nuxt/src/runtime/auth/api.session-verification.ts index 6de46341..4f985f41 100644 --- a/packages/nuxt/src/runtime/auth/api.session-verification.ts +++ b/packages/nuxt/src/runtime/auth/api.session-verification.ts @@ -19,7 +19,7 @@ export default defineEventHandler(async (event) => { const { token } = await readBody<{ token?: string }>(event) const runtimeConfig = useRuntimeConfig() - const adminApp = ensureAdminApp( + const adminApp = await ensureAdminApp( { // NOTE: ensured by the module projectId: runtimeConfig.public.vuefire!.config!.projectId, diff --git a/src/server/admin.ts b/src/server/admin.ts index 55892bed..df6f0e00 100644 --- a/src/server/admin.ts +++ b/src/server/admin.ts @@ -1,12 +1,28 @@ -import { - initializeApp as initializeAdminApp, - cert, - getApps as getAdminApps, - applicationDefault, - // renamed because there seems to be a global Credential type in vscode +// Dynamic imports to handle Nuxt 3.17.6+ Vite environment changes +import type { Credential as FirebaseAdminCredential, AppOptions, } from 'firebase-admin/app' + +let adminAppModule: any + +// Use dynamic import in development, static import in production +async function getFirebaseAdminApp() { + if (!adminAppModule) { + // Check if we're in development mode + const isDev = process.env.NODE_ENV !== 'production' + + if (isDev) { + // Use require in development mode to work around Vite environment changes + adminAppModule = require('firebase-admin/app') + } else { + // Use dynamic import in production + adminAppModule = await import('firebase-admin/app') + } + } + return adminAppModule +} + import { logger } from './logging' const FIREBASE_ADMIN_APP_NAME = 'vuefire-admin' @@ -18,14 +34,22 @@ const FIREBASE_ADMIN_APP_NAME = 'vuefire-admin' * @param name - name of the app * @experimental this is experimental and may change in the future */ -export function ensureAdminApp( +export async function ensureAdminApp( firebaseAdminOptions?: Omit, name = FIREBASE_ADMIN_APP_NAME ) { + // Get Firebase Admin SDK functions dynamically + const { + initializeApp: initializeAdminApp, + cert, + getApps: getAdminApps, + applicationDefault, + } = await getFirebaseAdminApp() + // only initialize the admin sdk once logger.debug(`Checking if admin app "${name}" exists...`) - if (!getAdminApps().find((app) => app.name === name)) { + if (!getAdminApps().find((app: any) => app.name === name)) { const { // these can be set by the user on other platforms FIREBASE_PROJECT_ID, @@ -120,5 +144,5 @@ export function ensureAdminApp( } // we know have a valid admin app - return getAdminApps().find((app) => app.name === name)! + return getAdminApps().find((app: any) => app.name === name)! } From a33cbb49f38e3eb14c28bdd4d503903d288fa0d5 Mon Sep 17 00:00:00 2001 From: Luca Grifo Date: Thu, 17 Jul 2025 21:14:13 +0200 Subject: [PATCH 2/2] fix(packages/nuxt/src/runtime/admin/plugin.server.ts): typescript warnings --- packages/nuxt/src/runtime/admin/plugin.server.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/nuxt/src/runtime/admin/plugin.server.ts b/packages/nuxt/src/runtime/admin/plugin.server.ts index 3a929314..028fab23 100644 --- a/packages/nuxt/src/runtime/admin/plugin.server.ts +++ b/packages/nuxt/src/runtime/admin/plugin.server.ts @@ -1,4 +1,4 @@ -import { type App as AdminApp } from 'firebase-admin/app' +import type { App as AdminApp } from 'firebase-admin/app' import { ensureAdminApp } from 'vuefire/server' import { defineNuxtPlugin, useRequestEvent, useRuntimeConfig } from '#imports' @@ -9,7 +9,9 @@ export default defineNuxtPlugin(async () => { const firebaseAdminApp = await ensureAdminApp(vuefire?.admin?.options) // TODO: Is this accessible within middlewares and api routes? or should we use a middleware to add it - event.context.firebaseApp = firebaseAdminApp + if (event) { + event.context.firebaseApp = firebaseAdminApp + } return { provide: {