I have been struggling to get a simple firebase callable function to work - I was constantly getting CORS errors:
"has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled."
This is the code:
import * as functions from 'firebase-functions';
export const sayHello = functions.https.onCall(() => {
return {
message: "Hello world"
};
})
it is called using this code:
const sayHello = firebase.functions().httpsCallable('sayHello');
sayHello().then((result) => {
console.log(res.data.message)
})
I came across this post:
CORS error on httpsCallable firebase in Create React App
The answer given is:
"The Cause:
Cloud functions were forbidding access to the function. Newly created functions did not have a Cloud Functions Invoker. This change was implemented on Jan 15, 2020
Solution:
Create Cloud Functions Invoker and set to allUsers. https://cloud.google.com/functions/docs/securing/managing-access-iam".
This worked for me with permission set to allUsers and role set to Cloud Functions Invoker.
I noticed that there is also an allAuthenticatedUsers role. It doesn't work if this is set to Cloud Functions Invoker or Cloud Functions Admin.
What does allAuthenticatedUsers mean? Does this mean that within my webapp a user has to sign in app.auth().signInWithEmailAndPassword or similar mechanism? This doesn't seem to work.