【问题标题】:Unable to authenticate against Firestore emulator无法针对 Firestore 模拟器进行身份验证
【发布时间】:2021-02-28 00:08:15
【问题描述】:

我正在使用 Firebase 管理员和 Node + Express 从 Appengine 更新 Firestore 文档。我想使用 Firestore 模拟器进行一些测试。这是我得到的错误:

Error in post /requests Error: {"servicePath":"localhost","port":8080,
"clientConfig":{},"fallback":true,"sslCreds":{"callCredentials":{}},
"projectId":"test-project","firebaseVersion":"9.4.1","libName":"gccl",
"libVersion":"4.7.1 fire/9.4.1","ssl":false,
"customHeaders":{"Authorization":"Bearer owner"},"scopes":[
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/datastore"
]}
You need to pass auth instance to use gRPC-fallback client in browser. Use OAuth2Client from google-auth-library.

在我打电话的每个测试之前:

var serviceAccount = require(process.env.FIREBASE_SA_CREDENTIAL);
firebaseAdmin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    projectId: 'test-project'
});

并且测试使用了一个简单地访问 Firestore 的类,如下所示:

this.db = firebaseAdmin.firestore();
...

我有以下 npm 依赖项:

"@google-cloud/firestore": "^4.7.1",
"firebase": "^8.0.2",
"firebase-admin": "^9.4.1"
"firebase-tools": "^8.16.2"

我正在启动模拟器并运行测试:

firebase emulators:exec 'jest --verbose=false'

我看不到配置中有什么不正确 - 据我所知,模拟器应该接受所有身份验证。错误消息表明它正在使用一些前端库而不是后端库,但依赖项似乎都是正确的。

【问题讨论】:

  • 出于好奇,您使用的是服务器端渲染吗?我发现我有这个问题,当我把SSR功能去掉后,我就可以按预期运行了。

标签: node.js firebase google-cloud-firestore


【解决方案1】:

好的,解决了。 jest config 中缺少重要的一行:

module.exports = {
  testEnvironment: 'node'
}

测试环境:'节点'。这触发了 Firebase 库中的一些行为,使他们认为环境是浏览器。

【讨论】:

    猜你喜欢
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    相关资源
    最近更新 更多