"FirebaseError: Unauthenticated" when call a "Callable Function" with "Emulator" - firebase

In production environment exists a lot of answers to adjust google cloud console, but for development with function emulator, there is nothing.
Using onRequest to call with HTTP works, but onCall with SDK I get "FirebaseError: Unauthenticated".

Works after set a App Check with reCAPTCHA v3 usign localhost and 127.0.0.1. Set different recaptcha for dev and prod environments.
Guide for web: https://firebase.google.com/docs/app-check/web/recaptcha-provider

Related

Firebase functions emulator requesting external network resource: computeMetadata

I have the firebase emulator running in a docker container locally for testing. The emulator includes everything I'm using for my app (firestore, auth, functions, storage) so that I can develop and test independently of the production environment.
However, I'm getting these warnings which are making me nervous:
functions: Beginning execution of "myFunction"
⚠ External network resource requested!
- URL: "http://---.---.---.---/computeMetadata/v1/instance"
- Be careful, this may be a production service.
I don't know what that URL is? Does it mean I've misconfigured something somewhere?
I'm also getting these warnings:
⚠ emulators: You are not currently authenticated so some features may not work correctly. Please run firebase login to authenticate the CLI.
⚠ functions: You are not signed in to the Firebase CLI. If you have authorized this machine using gcloud application-default credentials those may be discovered and used to access production services.
⚠ functions: Unable to fetch project Admin SDK configuration, Admin SDK behavior in Cloud Functions emulator may be incorrect.
But I don't think I want to authenticate, right? I don't want to touch anything to do with the live project on production while testing locally. Can I safely ignore these, or is there a good reason to authenticate?
The warnings are indicative that there had some issues while initialization during the setup for emulators .
Make sure that the emulator is installed by the following command: firebase setup:emulators:firestore, for this you can refer Documentation.
Deploy your function in the firebase in order to get recognized. you can refer to the Documentation using firebase deploy --only functions
Also to be sure please check your Firebase json and see if the local host is configured and not the production host,just to be sure.
For further reference you can follow up the stackoverflow thread Docker authentication issueand Firestore emulatorwhere a similar issue has been raised by other users which might be helpful.

Localhost Firebase Cloud Functions are working with the emulator but not live

I have a Firebase app with an App Check debug token implemented.
When I run my Cloud Functions using the Emulator they work fine.
But when I turn off the emulator and try to access the live deployed version from http://localhost:8080/ I get this console error:
POST http://localhost:5001/my-app/us-central1/getZohoDeskTicketsLoggedInUser net::ERR_CONNECTION_REFUSED
Why does this happen when calling the live version and not with the emulator?
There are no Firebase Cloud Function logs to provide because the functions never even fire.
Perhaps I need to white list the locahost domain somewhere?
When you deploy your web app to productions, you should use URL of deployed Cloud function to make your API requests instead of localhost which will lead to connection errors. You can additionally call connectFunctionsEmulator only when you are on localhost:
if (window.location.hostname === "localhost") {
connectFunctionsEmulator(...)
}

Firebase emulators blocking firebase functions?

So in my web app, I am calling an https callable cloud function like so
const createStripeCheckout = functions.httpsCallable('createStripeCheckout')
//Call function
And up until I started using the firebase emulator, everything was fine. It was making a request to the https endpoint, and the checkout was made successfully. But after I used functions in the emulator, it makes a request to the local dev server (localhost:5000/firebase-project/name) instead of the correct https endpoint hosted by firebase (firebase/name) and the function can't work unless I start the emulator, and will only work on my computer. I don't recall changing any settings.
Found the problem, in my code there was a line that read:
functions.useEmulators("localhost", 5001)
I guess that directed all functions to that adress

Unable to call Firebase function from React.js application

I have a React.js application powered by a number of Firebase functions and real time database standing behind them. It has been working without any issue for the past 2-3 months and now I am getting a warning on the functions logs which says that:
#firebase/database: FIREBASE WARNING: {"code":"app/invalid-credential","message":"Credential implementation provided to initializeApp() via the \"credential\" property failed to fetch a valid Google OAuth2 access token with the following error: \"Failed to parse access token response: Error: Server responded with status 404.\"."}
The way I initialize firebase from my React.js application looks like this:
I have double-checked everything standing behind process.env and it seems to be as expected. The website written in React.js in hosted under the Firebase hosting.
And this is how Firebase functions connect to Admin SDK:
I am not sure what would be the issue here. Nothing has changes in the code base from our side. Not sure if Firebase changed something internally that we need to consider.
What solved the issue for me was to go to the google cloud console (where Firebase functions are also available). Then I opened one of the failing cloud functions and I navigated to
My_Function_Name/Edit/RUNTIME, BUILD AND CONNECTIONS SETTINGS/RUNTIME SERVICE ACCOUNT/
And then I noticed there that for all of my functions it was selected App Engine Default Service Account instead of Firebase Admin SDK. I never explicitly set the runtime to this option. So, when I brought it back to Firebase Admin SDK the error was gone I was able to use the application once again.

Firebase: how to debug onCall functions?

I am using Google Cloud Functions Emulator for debugging locally my Firebase functions.
Firebase recommend to use functions.https.onCall functions instead of functions.https.onRequest for avoid using http calls directly. Instead Firebase recommend to use Firebase Functions SDK for call such functions from code.
For functions.https.onRequest I used http-trigger flag but how can I debug onCall functions ?
I found a way to run 'onCall' cloud functions locally and call them from a local client. I still haven't figured out how to attach a debugger but at least I can iterate faster and use console.log statements.
Use firebase serve to host the functions. Even though these are supposed to be for http requests I found that the the firebase clients have a property to override the host name for onCall functions. This takes care of authentication and passing in the right data and context objects for you.
From Terminal in the root of your firebase project folder:
firebase serve
You should get a successful result in the terminal window. Take the hostname and port number:
✔ functions: helloWorld: http://localhost:5000/your-project-name/us-central1/helloWorld
iOS client:
Functions.functions().useFunctionsEmulator(origin: "http://localhost:5000")
NOTE: you have to disable App Transport Security in iOS for this to work
NOTE: other clients probably have the same API.

Resources