How to connect Firebase Admin to Emulator Auth

Par*_*oos 7 firebase firebase-tools firebase-authentication firebase-admin

I am struggling to connect to the emulated Firebase Auth service via the Firebase Admin SDK. I cut down the code to really make the problem stand out, and hope someone can help.

This is the code of the test.js I run (in NodeJS):

// Someone said these two lines should allow the firebase-admin
// SDK to connect to the emulators, but... no.
process.env['GCLOUD_PROJECT'] = 'my-firebase-project-id'
process.env['FIRESTORE_EMULATOR_HOST'] = 'localhost:8080'
const admin = require('firebase-admin')
const app = admin.initializeApp()
const auth = app.auth()
console.log('I have an auth service object')
auth.listUsers().then(users => console.log(users))
Run Code Online (Sandbox Code Playgroud)

I run the emulators like this:

firebase emulators:start --only auth
Run Code Online (Sandbox Code Playgroud)

When I run the test.js file, I get this:

PS C:\...\functions> node .\test.js
I have an auth service object
(node:18232) UnhandledPromiseRejectionWarning: Error: Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "Error fetching access token: Error while making request: getaddrinfo EAI_AGAIN metadata.google.internal. Error code: EAI_AGAIN".
    at FirebaseAppError.FirebaseError [as constructor] (C:\...\functions\node_modules\firebase-admin\lib\utils\error.js:44:28)
    at FirebaseAppError.PrefixedFirebaseError [as constructor] (C:\...\functions\node_modules\firebase-admin\lib\utils\error.js:90:28)
    at new FirebaseAppError (C:\...\functions\node_modules\firebase-admin\lib\utils\error.js:125:28)    
    at C:\...\functions\node_modules\firebase-admin\lib\app\firebase-app.js:87:19
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
Run Code Online (Sandbox Code Playgroud)

I run this on Windows with the following versions of firebase:

    "firebase-admin":     "^10.0.2",
    "firebase-functions": "^3.18.1",
Run Code Online (Sandbox Code Playgroud)

I read about getting a secret credentials key and adding its path like this:

process.env['GOOGLE_APPLICATION_CREDENTIALS'] = 'C:\\...\\functions\\.runtimekey.json'
Run Code Online (Sandbox Code Playgroud)

And that 'works' in as much as I then can access the real cloud auth instance (as long as the emulators is off) but that isn't what I want. I want to connect firebase-admin and get a list of users in the emulated Auth instance.

Many thanks for any help you can offer!

Tja*_*ark 4

设置环境变量FIREBASE_AUTH_EMULATOR_HOST

export FIREBASE_AUTH_EMULATOR_HOST=localhost:9099
Run Code Online (Sandbox Code Playgroud)

包括协议方案(即http/https)

或者在你的情况下:

process.env['FIREBASE_AUTH_EMULATOR_HOST'] = 'localhost:9099'

然后您可以正常初始化应用程序

admin.initializeApp()

为我工作(在我最终弄清楚不包括协议方案之后)

来源:https ://firebase.google.com/docs/emulator-suite/connect_auth#admin_sdks