Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase: Error (auth/invalid-api-key) with demo project

I am trying to write some automated tests with the Firebase client sdk. These tests are supposed to use the Firebase Auth Emulator. In order to avoid screwing up production data, I am using the emulators with a demo project id (as described in the documentation).

I start the emulator with this command:

firebase emulators:start --project demo-test --only functions,firestore,storage,auth

Then in my tests, I initialize the app with:

import { initializeApp } from 'firebase/app'
import { getAuth, connectAuthEmulator } from 'firebase/auth'

const app = initializeApp({ projectId: 'demo-test' })
const auth = getAuth(app)
connectAuthEmulator(auth, 'http://localhost:9099')

When the test initializes, I get this error:

 FirebaseError: Firebase: Error (auth/invalid-api-key).

    > 45 |     const auth = getAuth(app)
         |                  ^

Note: This problem only occurs with authentication. I can successfully connect to other emulators like Firestore.

It seems that despite what the documentation says, I can't actually connect the client SDK to a demo project and use the auth emulator. Every time I try, it throws this error.

Does someone know how to set up a demo project emulator and connect the client sdk to the auth emulator?

like image 273
Austin Fay Avatar asked Jun 16 '26 22:06

Austin Fay


1 Answers

I put some random value in initializeApp and for some reason it worked...

initializeApp({
  projectId: 'demo-test',
  appId: 'test',
  apiKey: 'test',
})
like image 120
iwatachan Avatar answered Jun 18 '26 10:06

iwatachan