11

I am trying to list all users of my Firebase project using Firebase Admin SDK.
The below func to list users works fine when listing from Cloud Auth Service.

const admin = require('firebase-admin');
const serviceAccount = require('./certs/project-cert.json');

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
})

listAllUsers = (nextPageToken) => {
    // List batch of users, 1000 at a time.
    admin.auth().listUsers(1000, nextPageToken)
        .then(function(listUsersResult) {
            listUsersResult.users.forEach(function(userRecord) {
                console.log(userRecord.toJSON());
            });
            if (listUsersResult.pageToken) {
                // List next batch of users.
                listAllUsers(listUsersResult.pageToken);
            }
        })
        .catch(function(error) {
            console.log('Error listing users:', error);
        });
}

Then I set the ENV var to point to Emulator

export FIREBASE_AUTH_EMULATOR_HOST="localhost:9099"

And running the same listAllUsers func fails with following error:

FirebaseAppError: Error while making request: connect ECONNREFUSED ::1:9099. Error code: ECONNREFUSED
    at FirebaseAppError.FirebaseError [as constructor] (/Users/<me>/Work/pc/<project>/node_modules/firebase-admin/lib/utils/error.js:44:28)
    at FirebaseAppError.PrefixedFirebaseError [as constructor] (/Users/<me>/Work/pc/<project>/node_modules/firebase-admin/lib/utils/error.js:90:28)
    at new FirebaseAppError (/Users/<me>/Work/pc/<project>/node_modules/firebase-admin/lib/utils/error.js:125:28)
    at /Users/<me>/Work/pc/<project>/node_modules/firebase-admin/lib/utils/api-request.js:211:19
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  errorInfo: {
    code: 'app/network-error',
    message: 'Error while making request: connect ECONNREFUSED ::1:9099. Error code: ECONNREFUSED'
  },
  codePrefix: 'app'
}

I can see that the Emulator is running on port 9099, I can access it from http://localhost:4000/auth.
The iPhone emulator itself can access the Auth Emulator fine, but getting the connection error with Admin SDK

My env

macOS Monterey 12.3.1
"firebase-admin": "^10.2.0",
firebase-tools 10.9.2
node ver 17.6.0

firebase.json

  ...
  "emulators": {
    "auth": {
      "port": 9099
    },
  ...
kernelman
  • 992
  • 1
  • 13
  • 28
  • Try setting `FIREBASE_AUTH_EMULATOR_HOST` before calling initializeApp - does that solve the issue? – Chris Jun 29 '22 at 01:30

2 Answers2

20

Change your localhost to 127.0.0.1 or similar (in case you use container services like docker)

Change this:

export FIREBASE_AUTH_EMULATOR_HOST="localhost:9099"

To this:

export FIREBASE_AUTH_EMULATOR_HOST="127.0.0.1:9099"

Sometimes localhost and 127.0.0.1 act differently depending on your platform

I figured it out after I installed docker, and everything that depends on localhost stopped working

QDinh
  • 404
  • 4
  • 7
0

I ran into this issue as well while running my Ionic / Capacitor app, but only while trying to test from andriod and ios. It was working from web. I found I needed to set the firebase.json to point to my computer's IP address of 192.168.68.22:

{
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "emulators": {
    "auth": {
      "host": "192.168.86.22",
      "port": 9099
    },
    "firestore": {
      "host": "192.168.86.22",
      "port": 8080
    },
    "ui": {
      "enabled": true
    },
    "singleProjectMode": true
  }
}

Then, I made sure to use those values when calling connectAuthEmulator() or connectFirestoreEmulator():

import firebaseConfig from '../firebase.json';

const firestoreEmulatorHost = firebaseConfig.emulators.firestore.host
const firestoreEmulatorPort = firebaseConfig.emulators.firestore.port
const authEmulatorHost = firebaseConfig.emulators.auth.host
const authEmulatorPort = firebaseConfig.emulators.auth.port

export const firebaseApp: FirebaseApp = initializeApp({...})
const db = getFirestore()

connectAuthEmulator(auth, `http://${authEmulatorHost}:${authEmulatorPort}`, { disableWarnings: true })
connectFirestoreEmulator(db, firestoreEmulatorHost, firestoreEmulatorPort)

dhildreth
  • 637
  • 1
  • 6
  • 15