Firebase Admin会在连接到Auth Molulator时提供错误的错误

发布于 2025-02-10 07:02:38 字数 2404 浏览 0 评论 0原文

我正在尝试使用Firebase Admin SDK列出Firebase项目的所有用户。
从云验证服务列出时,列出用户列出的算法正常工作。

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

然后,我将env var设置为指向模拟器

export FIREBASE_AUTH_EMULATOR_HOST="localhost:9099"

并运行相同的listallusers func失败,但有以下错误:

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'
}

我可以看到模拟器在端口9099上运行,我可以从http:http: // localhost:4000/auth
iPhone模拟器本身可以访问auth Mimulator,但是使用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
    },
  ...

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
    },
  ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她说她爱他 2025-02-17 07:02:38

将您的本地主机更改为127.0.0.1.1&nbsp;或类似的(如果您使用像Docker这样的容器服务)

更改以下内容:

export FIREBASE_AUTH_EMULATOR_HOST="localhost:9099"

对此:

export FIREBASE_AUTH_EMULATOR_HOST="127.0.0.1:9099"

有时候,Localhost和127.0.0.1的行为不同,根据您的平台,

我在安装Docker和所有内容后都会发现它,以及所有内容这取决于localhost停止工作

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

猫瑾少女 2025-02-17 07:02:38

我在运行离子 /电容器应用程序时也遇到了这个问题,但仅在尝试从Andriod和iOS测试时。它是从网络上工作的。我发现我需要设置firebase.json以指向我的计算机的IP地址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
  }
}

然后,我确保在调用conder> connectauthemulator()时确保使用这些值。或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)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文