Handle CORS with cloud functions & reactfire functions SDK? #483
-
Hello, Would anyone know how to setup CORS correctly to use google cloud functions with the reactfire functions SDK? I've enabled CORS in my cloud function & allowed function invoke from allUsers, but cannot figure how to setup the preflight correctly Current code: React component: Cloud function:
This gives me Any ideas or good documentation on this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Fixed my issue. For any newbies struggling on CORS like me, here is how I fixed it:
|
Beta Was this translation helpful? Give feedback.
-
If you want to Allow all HTTP methods request to your google cloud function from the front end and face an issue with CORS. The Following template must work. It worked for me.
|
Beta Was this translation helpful? Give feedback.
Fixed my issue. For any newbies struggling on CORS like me, here is how I fixed it:
Make sure your function is callable from other domains - set "allUsers/allAuthenticatedUsers" permissions to Invoke cloud function on your function
Handle preflight options in your cloud function:
if request.method == 'OPTIONS': headers = { 'Access-Control-Allow-Origin': '*', # <- Allow your function to be called from any domain 'Access-Control-Allow-Methods': 'POST', # <- POST or any method type you need to call 'Access-Control-Allow-Headers': 'Content-Type, Authorization', # <- Authorization Header needed 'Access-Control-Max-Age': '3600', } return ('', 204, headers)
Allow Any origin after prefligh…