0

I am trying to call firebase function from my web app I am getting

access-Control-Allow-Origin error

My code is from web:

   var createNewUserAndEvent = firebase.functions().httpsCallable('createNewUserAndEvent');
   const data= await  createNewUserAndEvent(userData);

and from firebase function:

const functions = require("firebase-functions");
var admin = require("firebase-admin");
admin.initializeApp();
exports.createNewUserAndEvent = functions.https.onCall((data, context) => {
  cors({ origin: true })(data, context, async () => {
    if (!context.auth) {
Vitaly Menchikovsky
  • 7,684
  • 17
  • 57
  • 89
  • After reviewing the code you've sent as well as [this post](https://stackoverflow.com/questions/42755131/enabling-cors-in-cloud-functions-for-firebase) along with the links it provides; I don't believe your code is wrong but perhaps you need to check the configuration you have set up in Firebase and whether or not [the headers allow cors](https://firebase.google.com/docs/hosting/full-config#headers). Could you also include in your example the imports used? Just to discard the possible causes that are easier to test for. – fabc May 06 '21 at 14:01
  • @fabc My web isnt on firebase I am using netlify – Vitaly Menchikovsky May 06 '21 at 16:59
  • Pardon, but you've not tagged netlify, but you did tag firebase and are using the firebase library, hence my confusion. Still, for more context would you kindly provide us with the filetree of your project as well as your netlify settings? – fabc May 07 '21 at 08:53
  • @fabc I also did hosting on firebase and got same error,I updated my original question with imports – Vitaly Menchikovsky May 07 '21 at 15:37
  • That... doesn't quite answer my question? Nevertheless, reviewing it further I've noticed that you've not been specifying the region when calling the function, is it possible that you're calling from a region different to the one the function is hosted on? (this would give out a cors error, because Google's servers have their own config, so cross-region function-calling can be a little tricky) I was reminded of this thanks to this [SO post](https://stackoverflow.com/questions/50278537/firebase-callable-function-cors) – fabc May 10 '21 at 08:10
  • Oh, and I forgot to point this out, but if you're still using netlify there's some instructions on how to set up CORS on netlify [here](https://answers.netlify.com/t/access-control-allow-origin-policy/1813) – fabc May 10 '21 at 08:12
  • @fabc not using netlify on this project only firebase hosting – Vitaly Menchikovsky May 10 '21 at 09:50
  • ... While I understand that you're no longer using netlify, my questions regarding the filetree that you're using, as well as specifying the region as part of the call, still apply. Would you kindly answer those questions for the sake of clarity? – fabc May 10 '21 at 13:04

0 Answers0