How to connect Firebase Admin to Emulator Auth
Asked Answered
B

2

9

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))

I run the emulators like this:

firebase emulators:start --only auth

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)

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

    "firebase-admin":     "^10.0.2",
    "firebase-functions": "^3.18.1",

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

process.env['GOOGLE_APPLICATION_CREDENTIALS'] = 'C:\\...\\functions\\.runtimekey.json'

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!

Botha answered 25/2, 2022 at 16:29 Comment(1)
I have the same problem with connecting to Realtime Database emulation. However, for Cloud Functions, no big hoops should be needed. Please check github.com/akauppi/firebase-jest-testing/tree/master/sample . I'm not sure what I'm doing differently, but that repo should show you it can be done, and then it's just... seeing the diff.Buggery
T
6

Set the environment variable FIREBASE_AUTH_EMULATOR_HOST

export FIREBASE_AUTH_EMULATOR_HOST=localhost:9099

Do not include the protocol scheme (i.e http/https)

Or in your case:

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

Then you can initialise the app as per normal

admin.initializeApp()

Worked for me (after I finally figured out not to include the protocol scheme)

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

Thundercloud answered 21/7, 2022 at 9:46 Comment(0)
M
1

set auth emulator host env and pass projectId in initializeApp

const admin = require('firebase-admin');

process.env['GCLOUD_PROJECT'] = 'my-firebase-project-id';
process.env['FIRESTORE_EMULATOR_HOST'] = 'localhost:8080';
process.env['FIREBASE_AUTH_EMULATOR_HOST'] = 'localhost:9099'; // add this line

const app = admin.initializeApp({projectId: 'your-project-id'}); // change this line

const auth = app.auth();

console.log('I have an auth service object');

auth.listUsers().then(users => console.log(users));
Munguia answered 2/2, 2024 at 20:52 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.