deammer
deammer

Reputation: 229

How to emulate Firebase authentication without an API key

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?

Upvotes: 5

Views: 2466

Answers (3)

evnp
evnp

Reputation: 301

Let to the game here, but I've found I can avoid auth/invalid-api-key errors while using the emulators by simply passing apiKey: "any-string-value" to initializeApp in tests:

initializeApp({ apiKey: "test-api-key" });
connectAuthEmulator(getAuth(), 'http://localhost:8081');

This seems to be tracked in this (open, but inactive) issue:
https://github.com/firebase/firebase-js-sdk/issues/6776

The check for apiKey is done on the client side, which was appended before the auth emulator was created, so it is probably not needed. https://github.com/firebase/firebase-js-sdk/blob/master/packages/auth/src/core/auth/register.ts#L68-L71

Hope this is helpful to someone (very tricky to get answers to seemingly-simple things like this from the Firebase docs).

Upvotes: 0

papawapato
papawapato

Reputation: 31

Yes it is.

A demo Firebase project has no real Firebase configuration and no live resources. These projects are usually accessed via codelabs or other tutorials.

Project IDs for demo projects have the demo- prefix.

Firebase Docs

Run firebase emulators:start --project demo-[ANYTHING_YOU_WANT]

For example firebase emulators:start --project demo-test-project

Upvotes: 3

Doug Stevenson
Doug Stevenson

Reputation: 317392

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.

Upvotes: 2

Related Questions