3

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

aofdev
  • 1,772
  • 1
  • 15
  • 29
NDavis
  • 1,127
  • 2
  • 14
  • 23

2 Answers2

1

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": "*"
NDavis
  • 1,127
  • 2
  • 14
  • 23
0

Try following this ?

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

Handling CORS in Google Cloud Functions

Hope that helps!

aofdev
  • 1,772
  • 1
  • 15
  • 29
  • Yes those are good pages for reference. I couldn't get it working even with that help though... :/ – NDavis Sep 16 '17 at 21:03
  • you try insert ? "headers" : [ { "key" : "Access-Control-Allow-Origin", "value" : "*" } ] – aofdev Sep 17 '17 at 01:44
  • 1
    Yeah I tried that in like 5 different ways. But it would send a pre-flight OPTIONS request and that was never answered correctly I guess – NDavis Sep 17 '17 at 01:51
  • For those who try the methods mentioned in this post and still not working, try rechecking your request function. Mine was simple as removing content-type from headers from Fetch web api – Narongdej Sarnsuwan Apr 28 '20 at 14:31