Firebase realtime database warning via react native expo - firebase

I have just manually key in all the data in my firebase realtime database. But when i run my application (using react native expo), i get the following warning in my terminal:
[2023-02-06T09:53:41.782Z] #firebase/database: FIREBASE WARNING: Database lives in a different region. Please change your database URL to https://******-default-rtdb.asia-southeast1.firebasedatabase.app (https://*******-default-rtdb.firebaseio.com/)
What does this means? Does it means my firebase realtime database wont work at all?
Also I did a checked on my firebase realtime database URL, the url is the same as the one they ask me to change to before the URL in the bracket. What does this means? I am confused by this.
If I have to change the URL to the bracket one, how do i do it?
My current firebase config: I copied this from the firebase when I first started developing this app

Update following your comment and question update: It appears that your Firebase config object doesn't contain the URL of your Realtime Database instance.
You didn't share your Firebase config object but it seems that it contains the URL of Realtime Database instance based in the default region, i.e. the us-central1 region.
So you need to update your Firebase config object with the correct URL, corresponding to the asia-southeast1 region.
const firebaseConfig = {
apiKey: "API_KEY",
authDomain: "PROJECT_ID.firebaseapp.com",
// The value of `databaseURL` depends on the location of the database
databaseURL: "https://******-default-rtdb.asia-southeast1.firebasedatabase.app",
projectId: "PROJECT_ID",
// ...
};

Related

Firebase Emulator request to Google Cloud Speech to Text Api denied

I would like to test the Google Cloud Speech-to-Text API from within Firebase Emulators. I currently have a trigger set on Firebase Storage that automatically gets fired when I upload a file via the Emulator Storage UI. This makes a request to the Speech to Text API, but I keep getting a permission denied error, as follows:
Error: 7 PERMISSION_DENIED: Cloud Speech-to-Text API has not been used in project 563584335869 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/speech.googleapis.com/overview?project=563584335869 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
I understand that project 563584335869 is the Firebase Cli project.
I have set the following environment variables when starting the emulator:
export GCLOUD_PROJECT=my-actual-glcloud-project-id && export FIREBASE_AUTH_EMULATOR_HOST='localhost:9099' && export GOOGLE_APPLICATION_CREDENTIALS=./path/to/service-account.json &&
firebase emulators:start
The service_account.json key file is associated with a service_account that has the following roles, as demonstrated by running
gcloud projects get-iam-policy my_project_id --flatten="bindings[].members" --format='table(bindings.role)' --filter="bindings.members:serviceAccount:my_service_account#my_project_id.iam.gserviceaccount.com"
ROLE
roles/speech.admin
roles/storage.admin
roles/storage.objectAdmin
roles/storage.objectCreator
roles/storage.objectViewer
Since the credentials for the service account I am using should have admin access to the speech to text api, why do I keep getting a permission denied error when running from the emulator, and how can I fix it?
The project id 563584335869 is not yours. It is firebase-cli cloud project’s project-id. In this case, the problem is arising because you have to set your own configuration using your credentials or your key.
You can see below a code for NodeJS which I found in github[1] where it shows how to configure your authentication to use the API.
// Imports the Google Cloud client library
const textToSpeech = require('#google-cloud/text-to-speech');
// Create the auth config
const config = {
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
};
// Creates a client
const client = new textToSpeech.TextToSpeechClient(config);
[1]https://github.com/googleapis/nodejs-text-to-speech/issues/26
EDIT
There are different ways to set up your authentication for speech to text. One way to resolve this problem would be to add the same auth configuration as the Text-to-Speech and it should look something like this in your code.
// Imports the Google Cloud client library
const speech = require('#google-cloud/speech');
// Create the auth config
const authconfig = {
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
};
// Creates a client
const client = new speech.SpeechClient(authconfig);
Another way to solve this problem according to this Google Cloud Documentation[2] is to setup your authentication.
[2]https://cloud.google.com/speech-to-text/docs/libraries#setting_up_authentication

How to Change Firebase realtime database location?

I am currently building react application with firebase, i don't recall which location i setup for firestore but according to latest docs of firebase you could change location of firebase realtime database which i am unable to change from their website. Console log:-
#firebase/database: FIREBASE WARNING: Database lives in a different region. Please change your database URL to https://xxxxx-xxxx-default-rtdb.asia-southeast1.firebasedatabase.app (https://xxxxx-xxxx-default-rtdb.firebaseio.com/)
So, whenever i change url on the site and click go nothing works.
Try to set databaseUrl when you initialize the app.
firebase.initializeApp({
apiKey: 'key',
authDomain: 'domain',
projectId: 'id',
storageBucket: 'storage',
messagingSenderId: 'id',
appId: 'id',
databaseURL: 'paste-here-needed-database-url'
});
You can't change a location of a existing database. You would need to migrate it manually to a new one that you setup to the location you want. The migration is very easy. Just export the old database into a json file and import it to the new one. You can read more about it here.
There is no option to change the location of the firebase real-time database.
But there is a way to migrate,
You could create a new instance for the same project. Then you have to select the location that you need when creating the real-time database. After that, you have to export the old database and import it to the new one.

When creating a new web app within firebase the database URL is missing

When I setup a new web app in firebase I noticed that the databaseURL is missing in the config.
apiKey: "",
authDomain: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
measurementId: ""
Those are the only values I can get. I haven't created a realtime database. But I have no need to.
I am working with the firebase plugin
"firebase": "^8.2.7"
in Nuxt but it is complaining that the databaseURL is missing. I throws the following error
Missing or incomplete config for current environment 'development': key 'databaseURL' is required!
So should I either get a databaseURL from firebase or is it a problem on the nuxt side?
When you create a new project in Firebase, it no longer immediately creates a Realtime Database for that project. For that reason, the databaseURL key is no longer included in the config, unless you've created a Realtime Database instance in the Firebase console.
Most Firebase SDK have been updated to deal with the fact that this databaseURL key is now optional, but it looks like you're on an environment where it is still required.
The two simplest solutions I can think of are:
Manually add a databaseURL key to the config. Most likely you can leave the value as an empty string.
Go to the database panel in the Firebase console to create a Realtime Database, and download the updated config.
Of course, if you actually use the Realtime Database in your code, the second option is the only one that will work.

FirebaseError: Missing or insufficient permissions after deleting Google Service Accounts

Use Case
I am using Firebase JS SDK to access Cloud Firestore from Browser, so that I can push UI Logs and Errors into Firestore.
Issue
It was all working fine until I, accidentally, deleted most of the Service Accounts in my Google Cloud Platform inside APIs & Services → Credentials.
Below is the sample Config that we use for a Firebase Web App:
<script src="https://www.gstatic.com/firebasejs/7.13.1/firebase-app.js"></script>
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "AIzaSyCztfcT6k6yXXXXXXXXXXXXXXXXXX",
authDomain: "<some-domain>.firebaseapp.com",
databaseURL: "https://<some-domain>.firebaseio.com",
projectId: "<some-domain>",
storageBucket: "<some-domain>.appspot.com",
messagingSenderId: "877458876543",
appId: "1:877458876543:web:9a9287dee234cd655ab7f2"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
</script>
I even tried reading Firebase Service Accounts Overview, but could not decide which Service Account to use for Firestore. Then, I tried adding different Service Accounts according to my Project ID and giving them Owner permissions to my Project, but it didn't work.
Full Error I am getting in Browser's Console:
Uncaught (in promise) FirebaseError: Missing or insufficient permissions.
at new Hr (https://www.gstatic.com/firebasejs/7.8.2/firebase-firestore.js:1:48219)
at https://www.gstatic.com/firebasejs/7.8.2/firebase-firestore.js:1:378673
at wr.<anonymous> (https://www.gstatic.com/firebasejs/7.8.2/firebase-firestore.js:1:377569)
at Wt (https://www.gstatic.com/firebasejs/7.8.2/firebase-firestore.js:1:15221)
at wr.S.dispatchEvent (https://www.gstatic.com/firebasejs/7.8.2/firebase-firestore.js:1:16063)
at Er.ua (https://www.gstatic.com/firebasejs/7.8.2/firebase-firestore.js:1:45322)
at nr.S.Fa (https://www.gstatic.com/firebasejs/7.8.2/firebase-firestore.js:1:43229)
at Ge (https://www.gstatic.com/firebasejs/7.8.2/firebase-firestore.js:1:21453)
at qe (https://www.gstatic.com/firebasejs/7.8.2/firebase-firestore.js:1:20854)
at Me.S.Ja (https://www.gstatic.com/firebasejs/7.8.2/firebase-firestore.js:1:23264)
Actually, I want to cleanup my Service Accounts as there were many created by myself. Please guide in telling which Service Account to use and what suitable Permissions to give.
Firestore Security Rules depend on a service account named service-PROJECT_NUMBER#firebase-rules.iam.gserviceaccount.com with the role
roles/firebaserules.system. You can use gcloud to restore this account:
gcloud projects add-iam-policy-binding PROJECT_ID --member=serviceAccount:service-PROJECT_NUMBER#firebase-rules.iam.gserviceaccount.com --role=roles/firebaserules.system
To get the project number, see Identifying projects.

React Native with Expo: is firebase-admin necessary?

I need to fetch my firebase db before the authentication process.
My firebase rules are restricted to authenticated users for both read and write.
So I've installed firebase-admin.
in the App.js
import * as admin from 'firebase-admin';
and after initialised it
const serviceAccount = require("./pathtocredentials.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://myappdbname.firebaseio.com"
});
This is the error that I have:
The package at "node_modules\firebase-admin\lib\firebase-namespace.js"
attempted to import the Node standard library module "fs". It failed
because React Native does not include the Node standard library. Read
more at
https://docs.expo.io/versions/latest/introduction/faq/#can-i-use-nodejs-packages-with-expo
Failed building JavaScript bundle.
So I searched and ..."Since your app is running inside JS VM on either iPhone or Android, the only way to access filesystem is to use react-native-fs"
What I want is just to check in my db if a user exists before let it authenticate. Did I necesserily need firebase-admin and react-native-fs ?
firebase-admin is not meant for web frontend code. It's for backend code running nodejs. You won't be able to effectively use firebase-admin in a React or any browser environment. On top of that, it's not really necessary if you just want to query your database from your frontend.
All you really need to do is read data using the normal Firebase JavaScript API for web. You can tell if a node exists in the database by checking if its DataSnapshot object actually exists.

Resources