NDavis
NDavis

Reputation: 1227

Cloud Functions for Firebase not setting Access-Control-Allow-Origin

While attempting to get CORS working with Cloud Functions for Firebase, I tried to follow the official example here: https://github.com/firebase/functions-samples/tree/master/authorized-https-endpoint

However, even following the tutorial instructions as exactly as possible, I still get this error in the console:

No Access-Control-Allow-Origin Header

Upvotes: 4

Views: 6326

Answers (2)

NDavis
NDavis

Reputation: 1227

I never got CORS working, but the workaround I found was to use hosting redirects for the functions. That way, HTTP function calls are not cross site at all.

https://firebase.google.com/docs/hosting/functions (See "Direct Hosting requests to your function")

{
  "hosting": {
    "public": "public",

    // Add the following rewrites section *within* "hosting"
   "rewrites": [ {
      "source": "/api/bigben", "function": "bigben"
    } ]

  }
}

Note: I have to define a redirect for every endpoint. Is there a way to have general rewrites? e.g.

"source": "/api/*", "function": "*"

Upvotes: 1

aofdev
aofdev

Reputation: 1812

Try following this ?

https://stackoverflow.com/a/42756623/6650162

Handling CORS in Google Cloud Functions

Hope that helps!

Upvotes: 0

Related Questions