nuxt-community / firebase-module

🔥 Easily integrate Firebase into your Nuxt project. 🔥
https://firebase.nuxtjs.org
MIT License
642 stars 97 forks source link

unexpected token using firebase.services.auth.ssr.credential = true #632

Open tmountjr opened 2 years ago

tmountjr commented 2 years ago

Version

@nuxtjs/firebase: 8.2.2 firebase: 9.9.2 nuxt: 2.15.8

Reproduction Link

Repo with minimal code

Steps to reproduce

Error occurs running nuxt dev.

nuxt.config.js:

export default {
  ssr: true,
  head: { ... },
  css: [],
  plugins: [],
  components: true,
  buildModules: [
    '@nuxtjs/eslint-module',
    '@nuxtjs/vuetify'
  ],
  modules: [
    '@nuxtjs/pwa',
    '@nuxtjs/firebase'
  ],
  firebase: {
    config: {
      apiKey: process.env.FIREBASE_API_KEY,
      authDomain: process.env.FIREBASE_AUTH_DOMAIN,
      databaseUrl: process.env.FIREBASE_DATABASE_URL,
      projectId: process.env.FIREBASE_PROJECT_ID,
      storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
      messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID,
      appId: process.env.FIREBASE_APP_ID
    },
    services: {
      database: true,
      auth: {
        initialize: {
          onAuthStateChangedAction: 'onAuthStateChanged'
        },
        ssr: {
          credential: true,
          serverLogin: true
        }
      }
    }
  },
  pwa: {
    manifest: {
      lang: 'en'
    },
    workbox: {
      importScripts: ['/firebase-auth-sw.js'],
      dev: process.env.NODE_ENV === 'development'
    }
  }
}

Also have a .env file where those environment variables are spelled out, as well as setting GOOGLE_APPLICATION_CREDENTIALS to the admin key.

For firebase.services.auth.ssr.credential, I've tried both true and a full path to the credential file. In my testing, removing firebase.services.auth.ssr.serverLogin allows the app to build properly, but the admin credentials are not used on the server side when loading data.

What is Expected?

I'm running an app that uses Firebase Auth to make sure that the person logging in is authorized to see the data, and then Realtime DB to fetch/store the data. Right now the rules are set so everyone has read access and no one has write access (a separate console app with the admin SDK writes to the database). I want to use server-side authentication following this guide but changing firebase.services.auth.ssr from true to { credential: true, serverLogin: true } causes a webpack error during the build (exact error below).

What is actually happening?

Receive this console error:

✖ Client
  Compiled with some errors in 6.74s

✔ Server
  Compiled successfully in 3.92s

 ERROR  Failed to compile with 1 errors                                                                                                   friendly-errors 14:55:15

 ERROR  in ./node_modules/@nuxtjs/firebase/lib/utils/auth-ssr/ssr-auth-session-manager.js                                                 friendly-errors 14:55:15

Module parse failed: Unexpected token (113:57)                                                                                            friendly-errors 14:55:15
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| 
|       const instance =
>         this.firebase.apps.find((a) => a.name === name) ??
|         this.firebase.initializeApp(this.config, name)
| 
                                                                                                                                          friendly-errors 14:55:15
 @ ./.nuxt/firebase/app.js 39:19-92
 @ ./.nuxt/firebase/index.js
 @ ./.nuxt/index.js
 @ ./.nuxt/client.js
 @ multi ./node_modules/eventsource-polyfill/dist/browserify-eventsource.js (webpack)-hot-middleware/client.js?reload=true&timeout=30000&ansiColors=&overlayStyles=&path=%2F__webpack_hmr%2Fclient&name=client ./.nuxt/client.js
tmountjr commented 2 years ago

Did some further testing with an isolated repo that I could share (rather than one filled with my app-specific pages). Tested using both node 14 and 16 (the linked repo has a .nvmrc file pinning to 16.something) in case it was a weird artifact of my node version, still same error as before.