【问题标题】:Firebase Functions running locally not authenticating with Firebase Admin SDK本地运行的 Firebase 函数未使用 Firebase Admin SDK 进行身份验证
【发布时间】:2018-05-20 22:24:56
【问题描述】:

部署到 Firebase 项目时,我能够成功运行此代码。

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

admin.initializeApp();

exports.createUser = functions.https.onRequest((req, res) => {
  admin.auth().createUser({ uid: req.body.id })
    .then(user => {
      return res.send(user);
    })
    .catch(err => {
      return res.status(422).send({ error: err });
    });
});

这是 Firebase 的回复:

{
    "uid": "abcdefg1234",
    "emailVerified": false,
    "disabled": false,
    "metadata": {
        "lastSignInTime": null,
        "creationTime": "Sun, 20 May 2018 22:22:55 GMT"
    },
    "tokensValidAfterTime": "Sun, 20 May 2018 22:22:55 GMT",
    "providerData": []
}

但是,当我使用firebase serve 运行函数本地 并调用相同的函数(使用 Postman)时,我遇到了权限错误:

{
    "error": {
        "code": "auth/insufficient-permission",
        "message": "Credential implementation provided to initializeApp() via the \"credential\" property has insufficient permission to access the requested resource. See https://firebase.google.com/docs/admin/setup for details on how to authenticate this SDK with appropriate permissions."
    }
}

在本地运行的 Firebase Functions 实例上的 firebase-admin 身份验证设置是否与在正常的生产 Firebase 服务器上运行时不同?

规格:

 "dependencies": {
    "firebase-admin": "~5.12.0",
    "firebase-functions": "^1.0.1"
  }

【问题讨论】:

    标签: node.js firebase firebase-authentication google-cloud-functions firebase-admin


    【解决方案1】:

    进一步审查:

    https://firebase.google.com/docs/functions/local-emulator

    如果您希望您的函数测试与 Google API 或其他 Firebase API 通过 Firebase Admin SDK,您可能需要设置管理员 凭据

    Cloud Firestore 和实时数据库触发器已经有足够的 凭据,并且不需要额外的设置。

    所有其他 API,包括 Firebase API,例如身份验证和 FCM 或 Google API(例如 Cloud Translation 或 Cloud Speech)需要 本节中描述的设置步骤。这适用于您是否 使用函数 shell 或 firebase serve。

    所以问题的答案似乎是“是”,您可能需要不同的身份验证过程,具体取决于您访问的 Firebase 服务。

    【讨论】:

      猜你喜欢
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      • 2018-07-27
      • 2019-09-01
      • 1970-01-01
      • 2017-10-16
      • 2019-07-09
      相关资源
      最近更新 更多