Home > Blockchain >  Firebase Cloud Functions - Throw Auth Error
Firebase Cloud Functions - Throw Auth Error

Time:11-26

Is it possible to throw an Auth Error from HTTPs callble functions?

I mean, instead of this

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

something like

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

      ...
   }

CodePudding user response:

Callable Functions should return an instance of HttpsError which requires gRPC error codes so the details of the error are properly transmitted to calling clients. If you throw a different error type, the client will only see a HttpsError with the code and message "internal" - no specifics will be sent to the client for safety.

If you want to pass through the error code of a Firebase Error, you can do so using the third argument. Also consider using "failed-precondition" (preferred) or "already-exists" (if it's a resource) instead.

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