vuejs / vuefire

🔥 Firebase bindings for Vue.js
https://vuefire.vuejs.org
MIT License
3.82k stars 323 forks source link

Currently you are not able to use auth module without a service account #1510

Closed tux2nicolae closed 3 months ago

tux2nicolae commented 3 months ago

Reproduction

https://github.com/vuejs/vuefire/blob/687ce0991c10cd7bae535ac61c329f4a1e7c9bb7/packages/nuxt/src/runtime/auth/plugin-authenticate-user.server.ts#L20

Steps to reproduce the bug

  1. Enable auth module

    vuefire: {
    auth: {
      enabled: true,
    },
    ...
    }
  2. Make sure you don't have the GOOGLE_APPLICATION_CREDENTIALS set

  3. Run npm run dev or build for production

Expected behavior

You should be allowed to use auth module without a service account

Actual behavior

You get this error when using the auth module without a service account specified.

The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services


I think the problem comes from here

https://github.com/vuejs/vuefire/blob/687ce0991c10cd7bae535ac61c329f4a1e7c9bb7/packages/nuxt/src/runtime/auth/plugin-authenticate-user.server.ts#L20

the getAdminAuth() is always called regardless of the fact you may not have a service account.

If you move the const adminAuth = getAdminAuth(firebaseAdminApp) inside the if block if (auth.currentUser?.uid !== uid) the problem goes away, or maybe it should be here https://github.com/vuejs/vuefire/blob/687ce0991c10cd7bae535ac61c329f4a1e7c9bb7/packages/nuxt/src/module.ts#L203

to add a if block if (nuxt.options.ssr && hasServiceAccount)

Additional information

Our use case is that, we don't have a service account because we don't need to render anything on the server based on authentication state, we are only using authentication on client side in some <client-only /> components