0

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.

jmc42
  • 359
  • 5
  • 22
  • Did you try to deploy this before upgrading the project to Blaze? – Stratubas Mar 22 '21 at 16:35
  • How do you call the Cloud Function from your client (your app)? – Renaud Tarnec Mar 22 '21 at 16:53
  • @Stratubas it was deployed after I upgraded to Blaze. I have also deleted and redeployed it but it makes no difference. It is called using httpsCallable. I will add the code. – jmc42 Mar 22 '21 at 18:50
  • Do you have the latest firebase-tools? What node version are you using? – Stratubas Mar 22 '21 at 19:31
  • Try deleting the function from google cloud console (you can go there from the "view detailed usage" option from firebase console functions dashboard) – Stratubas Mar 22 '21 at 19:33
  • I am using node 12 and the latest tools. I’ve deleted the function and redeployed but it made no difference. – jmc42 Mar 22 '21 at 20:33
  • I think that the that the allathenticatedusers range works on all authenticated users (you can authenticate your users as mentioned on [here](https://cloud.google.com/functions/docs/securing/authenticating#end-users)) – Luis Manuel Mar 25 '21 at 00:23

0 Answers0