【问题标题】:Firebase Cloud Functions - Throw Auth ErrorFirebase 云函数 - 引发身份验证错误
【发布时间】:2022-01-03 22:03:24
【问题描述】:

是否可以从 HTTPs 可调用函数中引发 Auth 错误?

我的意思是,而不是这个

 if (err.code === "auth/email-already-exists") {
    throw new functions.https.HttpsError(
      "invalid-argument",
      "The email address is already in use by other account"
    );
  }

类似

exports.signUp = functions
  .region("us-central1")
  .runWith({ memory: "2GB", timeoutSeconds: 120 })
   .https.onCall(async (data, context) => {
      ...

      if (err.code === "auth/email-already-exists") {
        throw err;
      }

      ...
   }

【问题讨论】:

    标签: firebase firebase-authentication google-cloud-functions


    【解决方案1】:

    Callable Functions 应该返回一个HttpsError 的实例,它需要gRPC error codes,以便错误的详细信息正确地传输给调用客户端。如果您抛出不同的错误类型,客户端将只会看到带有代码和消息"internal"HttpsError - 为了安全起见,不会向客户端发送任何细节。

    如果您想传递 Firebase 错误的错误代码,可以使用第三个参数来实现。还可以考虑改用"failed-precondition"(首选)或"already-exists"(如果它是资源)。

    if (err.code === "auth/email-already-exists") {
        throw new functions.https.HttpsError(
          "invalid-argument",
          "The email address is already in use by other account",
          { code: err.code }
        );
      }
    

    【讨论】:

      猜你喜欢
      • 2017-06-18
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多