Firebase Callable Function + CORS

firebase functions cors
firebase callable functions
firebase-functions express
firebase cors
firebase-functions oncall
functions.https.oncall cors
firebase callable functions example
firebase callable error

I'm trying to call a callable Cloud Function from my app, but I'm facing CORS issues.

I can't enable Cors since I don't have access to the request and response on the onCall function. This is my function:

exports.UserInvitation = functions.https.onCall((data, context) => {
  const email = data.email


  return new Promise((resolve, reject) => {
    admin.auth().createUser({
      email: email,
      emailVerified: false,
      password: password
    }).then(resolve).catch((err) => {
      console.error(err.code)
      reject(new functions.https.HttpsError(err.code, err.message))
    })
  })
})

And this is how I call it:

functions.httpsCallable('UserInvitation')({ email: this.input.value }).then((data) => {
      console.log('Sent invitation:', data)
})

Firebase-functions package.json:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "bluebird": "^3.5.1",
    "firebase-admin": "~5.12.0",
    "firebase-functions": "^1.0.1"
  },
  "private": true
}

WEB SDK Firebase version: 4.13.1

The status code passed to HttpsError cannot be test/code. It must be one of the standard codes listed here: https://firebase.google.com/docs/reference/functions/functions.https.HttpsError

Callable functions + httpsCallable + Cors on localhost · Issue #2077 , But I think there's room for documentation improvement somewhere in the Firebase doc on Callable functions. Version info Angular: 7.2.9  The Cloud Functions for Firebase client SDKs let you call functions directly from a Firebase app. To call a function from your app in this way, write and deploy an HTTPS Callable function in Cloud

My issue was not with CORS, but with my error handler. Instead of rejecting the promise, I've used throw directly in the catch handler.

catch((err) => {
  throw new functions.https.HttpsError('unknown', err.message, err)
})

Firebase Callable Function + CORS, The status code passed to HttpsError cannot be test/code . It must be one of the standard codes listed here:  Had the same issue and found the right solution for the region problem. If you defined a region on the backend (for example europe-west1) you need to define the same region when calling that function with:

I had this problem with some of my Firebase functions but not others. I eventually fixed it by doing this:

  1. Commenting the function out in index.js
  2. Running firebase deploy
  3. Entering y at the prompt to delete the function
  4. Waiting for 5 to 10 minutes
  5. Removing the comments around the function and running firebase deploy again

Without any changes to the function itself, it now runs without any CORS issues.

Callable.call example in Swift to encode - Firebase, cloud functions const functions = require('firebase-functions'); const REGION functions.httpsCallable("workingFunction"); workingFunctionAPI().then(result  Firebase Functions are just simple function. Yes, they can be use as HTTP endpoint etc, but they go sleep after being trigger and completed the job. The thing also is when I add this CORS package, I add it as middleware.

Call functions via HTTP requests, I wrote a callable function to invite new users for my app: How can I fix this if I don't have access to req/res objects inside onCall, to enable cors? that points to this : https://firebase.google.com/docs/functions/local-emulator where there is no  data - The value returned by the function. This can be any valid JSON value. The firebase-functions SDK automatically encodes the value returned by the user into this JSON format. The client SDKs automatically decode these params into native types according to the serialization format described below. If other fields are present, they should be ignored.

CORS issue with one cloud function but not another, You can give this one a read. It's unlikely to be a CORS issue for onCall function.. https://stackoverflow.com/questions/50278537/firebase-callable-function-cors. Firebase Functions CORS issue? Am trying to make my first web app and am running to this problem. Am testing my react app using localhost and am trying to console log my cloud function

Add Cors to callable function, In lesson 101, it teaches how to call firebase functions with httpsCallable. It's not working for me. When I try to call my callable firebase function,  Caution: New HTTP and HTTP callable functions deployed with any Firebase CLI lower than version 7.7.0 are private by default and throw HTTP 403 errors when invoked.

Comments
  • CORS should be automatically handled by the onCall handler. I suspect that the error message about CORS is inaccurate, and a result of the function returning 500 internal. What do you see in the Functions Logs in the Firebase Console?
  • You're right, the problem is with my error handler. The documentation says I can reject a promise with a instance of functions.https.HttpsError, but even though I'm doing that I'm getting a 500 error on the client. How can I fix that?
  • Even if I remove my return statement and add a throw new functions.https.HttpsError('test/code', 'error message') it only returns a object with message and status equals to "INTERNAL"
  • Ah yes, the status code passed to HttpsError cannot be test/code. It must be one of the standard codes listed here: firebase.google.com/docs/reference/functions/…
  • @bklimt please create an answer with this comment and I'll accept it :) I believe this detail should be added to the firebase onCall documentation