Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Firebase Cloud Functions – Throw Auth Error

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

      ...
   }

>Solution :

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 }
    );
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading