Home > Software engineering >  How to emulate Firebase authentication without an API key
How to emulate Firebase authentication without an API key

Time:05-13

I'm building an open-source project using Firebase's JS SDK. My goal is to allow contributors to run the project locally using the Firebase emulator so that they don't need any real credentials. The Firebase emulator docs specify that "you can run the emulators without ever creating a Firebase project". That's exactly what I want!

After running firebase init, I wrote up the following code. It triggers a popup that allows users to sign in through GitHub:

import { initializeApp } from "firebase/app";
import { connectAuthEmulator, getAuth, GithubAuthProvider } from "firebase/auth";

const app = initializeApp({
  projectId: "demo-project",
});

const auth = getAuth(app);
connectAuthEmulator(auth, "http://localhost:9099");

// When users sign in, we call the following method:
async function signIn() {
  const githubAuth = new GithubAuthProvider();
  await signInWithPopup(firebaseClientAuth, githubAuth);
}

The code above will trigger the following error:

Uncaught (in promise) FirebaseError: Firebase: Error (auth/invalid-api-key)

In the real world, I would call initializeApp() with an apiKey, but here I just want to emulate authentication. I've also tried not to call initializeApp() at all and call getAuth() without any arguments, but it triggers the same error.

Presumably, an API key requires creating a project, so is it actually possible to run the Firebase auth emulator without creating a Firebase project?

CodePudding user response:

is it actually possible to run the Firebase auth emulator without creating a Firebase project?

No, you need to have the emulator connected to a project. You'll notice in the setup instructions you are required to run firebase init to choose your project and the products you want to use in that project.

  • Related