三行代码  ›  专栏  ›  技术社区  ›  Raul

Firebase云函数-抛出身份验证错误

  •  0
  • Raul  · 技术社区  · 5 天前

    是否可能从HTTPs可调用函数引发身份验证错误?

     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;
          }
    
          ...
       }
    
    1 回复  |  直到 5 天前
        1
  •  1
  •   samthecodingman    5 天前

    HttpsError 这需要 gRPC error codes HttpsError 带着代码和信息 "internal" -为了安全起见,不会向客户发送任何详细信息。

    如果要传递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 }
        );
      }