How to let a locally-emulated Firebase Function hit real remote Firestore? - firebase

Due to there still being no support for web client reads from locally-emulated firestore at the moment, my best option for some parts of development is to allow the local functions to modify the real Firestore (in a separate dev project).
I tried the "Admin SDK configuration snippet", but it doesn't work, the reason I suppose being the mocked firebase-admin.
Has anyone documented doing this properly?

Related

How to Fix Permission Issues in Firebase Cloud Functions (Nextjs Experimental Hosting)

I have an issue related to Firestore, Hosting, and Cloud functions. I deployed a Nextjs application using the just-released experimental web framework support. I'm using Nextjs's Image API, and as expected, the Firebase CLI creates a cloud function for the project. Everything works fine at the start, but when I try to open a page in the application that reads some data from Firestore, it redirects to an auth page, which is unexpected.
Even when I auth with my Google account (since that's the logged-in user to the application), it returns a forbidden error and logs me out.
When I go back and try again, it returns a different error (most likely because I was logged out automatically).
I tried doing some research, and some people suggested adding an allUsers principal permission to Cloud functions, but that didn't work (Error: Principals of type allUsers and allAuthenticatedUsers cannot be added to this resource), and that's even insecure permission. Only authenticated users should be able to read data from the page as already configured in my Firestore security rules. So it's unclear what I need to do since Firebase created the Cloud function automatically or if this is related to my security rules and cloud functions.
What could be wrong? Everything works fine in my existing deployment setup to Netlify (where Edge functions are created automatically using their Nextjs plugin). I'm only trying to test the new Firebase web framework hosting features. Please let me know if you'd need me to provide some more context or debug files to better help you help me.
Thank you!
The message “ App requesting permission to access your google account “ pops up if the function runs in any region other than us-central1.
Currently, Firebase Hosting does not support Cloud Functions in any other regions, Except us-central1.
You can refer to this StackOverflow thread.

How to use Firebase (or Firestore) as an intermediary between a desktop app and an external API endpoint?

I have a desktop app that will be distributed to users, and part of its code (which the user might be able to access) has to perform an API call to a third-party web service. However, this requires the API keys to be embedded into the source code, which causes some obvious security issues.
The main solution I've found while researching on the subject is to have a backend server between the user and the third-party API endpoint. So, I've been looking into Firebase for a while and it seems that this solution can be implemented using Firestore and Cloud Functions.
But, I wasn't able to find any clear examples that achieve something like this, and since I have no previous experience with Firebase or just backend programming in general, I would like to know if I'm on the right track and get more details about how to get it done, or if there are better alternatives for solving this issue. Any input is appreciated, thx.
You can use the firebase cloud functions as you mentioned. Your desktop application will be communicating with the cloud function - simple API call. Here you can find examples how to implement and deploy functions and test it locally https://firebase.google.com/docs/functions/get-started. Your cloud function will hold the API keys (it is backend so it will be secure if you dont expose it explicitly. Also the backend to backend communication is considered as secure). Finally, this cloud function will call the third party service and return some data back to the desktop app if needed. So you are on the right track, that is how the design should look like. If you need some help with cloud functions, you can also contact me directly.

How to use GoogleDrive from Android app using FireBase Auth UI?

I have implemented Firebase Authentication in my app, using the Google Provider and the "https://www.googleapis.com/auth/drive.appdata" scope so my app can access its application folder within Google Drive
This part works fine and I retrieve the FireBaseUser once the authentication completes
What I now want to do is to access my app storage on Google Drive, but I don't know how to do this using the result of the current authentication
I tried to follow this: https://github.com/gsuitedevs/java-samples/blob/master/drive/quickstart/src/main/java/DriveQuickstart.java
But this doesn't work on Android.
The main issue is how to create the Credentials object
Do you have any idea on how to initialize a Drive.Builder instance so I can write/read to the app Google Drive folder?
Thanks
This cannot be done directly, because the Firebase & Google login are not the same, even when having logged in to Firebase with a Google account. On Android one meanwhile only has the "last logged in Google account" available for the user's Drive (the one on the device, which has nothing to do with Firebase Authentication, where Google may only be used as an "Authentication Provider").
So there are generally two options available to you:
Grant a GCP service-account access to Drive API and use Cloud Functions to access it.
Instead use Cloud Storage, which is within the Firebase eco-system and is similar to Drive.
I could also think of a combined solution approach, where users would use Cloud Storage (together with Firebase Authentication) and having a Cloud Function which uses a service-account, which eg. copies or moves uploaded files into a folder your Drive.
Concerning that (obviously server-side) Java example, the credentials.json clearly hints for a service account... and this is exactly where you can obtain this file from. However, for Android this is pretty useless - because it has major security implications, to package service-account credentials in an easy to de-compile package and distribute it on the WWW (to everybody). The Google Play Store likely would not permit you to publish or even upload that, because there are security checks in place. You could in best case only deploy that code as App Engine module, but not as an Android module.
Sorry for having destroyed your delusions and for not being able to provide a ready-made solution for 500 imaginary internet points, which pay nothing - but at least I can tell what is technically possible and what isn't - which effectively might save you lots of time, trying to accomplish the impossible.

maintain connection status of user with firestore. using firebase db and cloud function

I am trying to maintain connection status of user with firestore . but i don't understand some points.
Link : https://firebase.google.com/docs/firestore/solutions/presence
i refer the above link description but i don't understand how to write code.
see the below given topics:
Using presence in Realtime Database
where to write this given code in application or Cloud functions?
2.Connecting to Cloud Firestore
Updating Cloud Firestore's local cache
where to write local cache update code?
I use this in my android application.
Each section of the code in the Build Presence in Cloud Firestore is marked.
If it is marked with WEB, you'll need to run this code in your web application. If your clients are native Android or iOS, you'll need to write and run similar code for those platforms.
If the code is marked with NODE.JS, you will need to run this code in a Node.js environment. It should be possible to accomplish the same in Cloud Functions, but no sample is provided for that.

How to run tests with code that depends on plugins?

I want to test my Firebase data access layer with direct access to Firebase.
Is flutter drive the only way to run such tests, or are there other ways like running in emulator using flutter test?
What are the possible approaches and how can I set them up?
I'm not aware of a way to talk to a real database with flutter test currently, because there's no Firebase SDK to do the talking to the network. You probably wouldn't want this anyway, because it could make your tests nondeterministic or flaky if the data changes or the Firebase servers aren't accessible.
Some plugins, like shared_preferences, have an API for providing mock values. You could do something like that for the firebase_database plugin, either as a pull request to the first-party repo or by calling MethodChannel.setMockMethodCallHandler and BinaryMessages.handlePlatformMessage in your test code to simulate what the native side would be doing.

Resources