KEMBAR78
How to connect `firebase-admin` to the firebase emulators? · Issue #776 · firebase/firebase-admin-node · GitHub
Skip to content

How to connect firebase-admin to the firebase emulators? #776

@ldiqual

Description

@ldiqual

There is a related issue (#575) but it seems that it's still unresolved.

We would like to connect the admin SDK to firebase emulators when running tests. Our setup code looks like this:

import * as admin from 'firebase-admin'
admin.initializeApp({ credential: ... })
export default admin

Then in every file that needs access to firestore, we use:

import firebase from `./firebase`
firebase.firestore().doSomething()

How can we modify this setup so that is connects to the firebase emulators instead?

This is what we've tried so far:

  • Have the setup code return the @firebase/testing namespace depending on the node environment. However this library doesn't setup a default firebase app so calling firebase.firestore() fails.
  • Have firebase-admin connect to the emulators directly, by providing an explicit databaseURL and firestore URLs, and overriding credential.getAccessToken() to return owner. Now the issue is that the admin's implementation of firestore expects a service account and throws on the credentials override: https://github.com/firebase/firebase-admin-node/blob/master/src/firestore/firestore.ts#L95-L100
  • Return the firebase app as part of the setup code, so that app.firestore() always works. This requires refactoring the whole codebase to use an app instance instead of a firebase workspace.

Is there a recommended way to connect the admin sdk to emulators?

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions