Two Google Firebase hosted apps need to share one RTDB database - firebase

I created a project and built an app with that same name to work with a RTDB data base. Without knowing better I created another project for another app. But I want the second app in the second project to share the RTDB in the first project. It looks like I should have created the second app in the first project. How can I get both apps together to share the data base?

As per the documentation you can use a different app to access multiple realtime databases.
For example with the JavaScript API:
https://firebase.google.com/docs/reference/js/firebase.database.html
// Get the Database service for the default app
var defaultDatabase = firebase.database();
// Get the Database service for a specific app
var otherDatabase = firebase.database(app);
Note when creating app you can specify which configuration (database to load):
var firebaseConfig = {
apiKey: 'xxxx',
authDomain: 'xxxx',
databaseURL: 'https://xxx.firebaseio.com',
projectId: 'xxx'
};
var app = firebase.initializeApp(firebaseConfig);
For non-JavaScript consider referring to the following:
Android: How to connect to more than one firebase database from an android App
Android: one firebase database to two diff apps
Swift: 2 apps with the same database in Firebase
Regardless of language consider checking the API and selecting the language of your choice etc:
https://firebase.google.com/docs/reference

Using information from both answers, I have gotten 80% of the way to a solution. First, as a newcomer to Firebase I created a project there using the name of my existing web app that was hosted on Azure, so it could use the Firebase RTDB. Then later I hosted the web app there using that same name. I thought the project and the app were the same thing. So when I built a second app I did the same and created a project with the app name and hosted the app there. Then I had a problem when I wanted the 2nd app to read from the RTDB used by the 1st app. Looking through the documentation as suggested by Dean I tried "var otherDatabase=firebase.database(app)", but had to guess just what "app" was. I tried several guesses, none of which worked, as it seems that approach is for different apps in a common project. And I learned that neither of my projects even had an app! So then using the approach suggested by Doug and Frank, I finally found the Delete button for projects. So I deleted the 2nd project, added my two web apps as apps to the first project, got the new Firebase configuration for the second app and finally that app can access the RTDB. The "firebase deploy" command works as before for the 1st app, but does not work for the 2nd app. Still working on that problem.

Related

Flutter Firebase/Admob with multiple environments

I have two firebase project, one for dev and one for my live environment.
The same goes for the android configuration, one "dev" and one "live" environment.
Now im trying to use the flutter Firebase Admob package.
Each environment has a different app id like com.myapp.dev and com.myapp.live
I think on the app mob site only com.myapp.live is available. Do I need as well here two configs or can't I just show test ads without it?
Or how the connection works?
If I copy the Admob app id (probably related to com.myapp.live?) and use it in the android manifest under the main and/or the "dev" env folder I get always an error that Admob is not configured correctly.
Every other firebase service works fine with the google.services split-up in the different env folders.
EDIT:
I created a new Admob entry for com.myapp.dev, linked it with firebase and it still not work when im using the new Admob app id.

Firebase Remote Config - copy to another project

I have two projects for dev and prod. I want to be able to run a script to copy dev config to prod.
Firebase Remote Config has an API for programatically updating Remote Config. But as far as I can tell, you need to init admin with a project-specific service account. It seems like I would need two admin instances, but I'm not sure that's possible?
I'm wondering if someone has done this before and has an example script. Thanks!
See docs:
https://firebase.google.com/docs/remote-config/automate-rc
There is no Firebase Admin SDK for Flutter, so you'll have to implement this on a different platform that is supported. For a list of these platforms and instructions on setting it up, see the documentation on adding Firebase to a server.
For these platforms that the Firebase Admin SDK targets, you can create multiple instances of the FirebaseApp class, and initialize each of them with different credentials and project configuration. For examples of how to do this, see the documentation on initializing multiple apps.

The custom token corresponds to a different audience

I'm using firebase for authentication on my app, while developing I created a firebase project which I was using for that purpose, but now I've to sync it with another app. So on updating my google-services.json I'm getting the below error and am not able to connect to firebase.
The custom token corresponds to a different audience. [App ID does not match requested prject.]
How can I migrate my project to another one??
Try clearing the app data, it may be loading something from storage which is related to the other project.
I finally got the app working, basically firebase does not allow two projects with similar configuration. So in my case the test project I created while development had SHA key of my app configured, becasue of which I was getting the error.
I deleted that key and the conflict got resolved.
This is mainly in cases when you're using modules like Authentication where SHA key is used to identify the app.

Different environments on Firebase web application

I am building a web application with Firebase. Currently I can say that I do have two stages - development, the firebase serve which runs the localhost and firebase deploy --only hosting which uploads the web application on Firebase hosting.
Everything is fine with that, but I do not see this as a professional solution. The problem that I see is that, my local environment and the live web application share the same database. I did quite some research on the topic and I understood that there is no way to have two databases per one project on Firebase. The solution that is offered out there, is to create two projects on Firebase, one for development and one for production. Or even if you want to, one for staging.
This solution seemed completely fine with me. It's a good idea for sure. Couple of projects, for couple of environments, separate databases, just perfect. Then just before implementing this solution another problem bumped in my head. If I say, let's create a staging project, in order to serve me as a staging environment, and I decide to deploy my web application, the staging web application will be publicly available, so it will also get indexed by Google and so on.
So, what could you advice me in this situation? How can I make sure that my staging web application (hosted on the staging Firebase app) will not be available for others and will not be indexed by search engines. I thought about white-listing IPs or VPC, but I have no clue how to proceed in a way that is free and reliable.
In case anyone has this question, there's an article on the Firebase Blog about this.
Note: This Firebase article assumes that you have already created a second Firebase project for this new environment (i.e. project-dev), and have copied the config details into your working env (i.e. project-dev). Master and dev are two different env, so it makes sense to have two different Firebase configs.
The article states:
Fortunately for us, the Firebase CLI makes it simple to setup and deploy to multiple environments.
Adding and switching between environments with the Firebase CLI is as
simple as one command: firebase use.
$ firebase use --add
This command prompts you to choose from one of your existing projects
Select the project you want to use for a different environment, and then give it an alias. The alias can really be whatever you want, but it’s common to use aliases like “development”, “staging”, or “production”.
Once you’ve created a new alias, it will be set as the current
environment for deployment. Running firebase deploy will deploy your
app to that environment.
Switching environments
If you want to switch to another environment, just provide the alias in the use command.
$ firebase use default # sets environment to the default alias
$ firebase use staging # sets environment to the staging alias
For a single command, you can also specify the environment using the -P flag:
$ firebase deploy -P staging # deploy to staging alias
Hope that helps!
Edit: The following solution is for Firebase "Realtime Database". It does not apply to "Firestore". Read the difference here.
1. Firebase Realtime Databases Sharding
Now (2018 March), Firebase Realtime Database allows you to create multiple instance.
Official Document: Scale with Multiple Databases
Go to your Firebase Project
In the Firebase console, go to the Data tab in the Develop > Database section.
Select Create new database from the menu in the Databases section (upper right corner).
Customize your Database reference and Security rules, then click Got it.
(Optional) Modify the Security rule and Backup option of the new instance.
2. Usage
// Get the default database instance for an app
var database = firebase.database();
// Get a secondary database instance by URL
var database = firebase.database('https://testapp-1234.firebaseio.com');
3. Example Usage: Different Environment
firebase-config.js
const BUILD_LEVEL = "dev";
// const BUILD_LEVEL = 'stage'
// const BUILD_LEVEL = 'prod'
let config = {
apiKey: "your_apiKey",
authDomain: "your_authDomain",
projectId: "your_projectId",
storageBucket: "your_storageBucket",
messagingSenderId: "your_messagingSenderId"
};
if (BUILD_LEVEL === "dev") {
config.databaseURL = "https://your-project-dev.firebaseio.com/";
} else if (BUILD_LEVEL === "stage") {
config.databaseURL = "https://your-project-stage.firebaseio.com";
} else if (BUILD_LEVEL === "prod") {
config.databaseURL = "https://your-project-dev.firebaseio.com";
}
firebase.initializeApp(config);
Now to change the Firebase Database instance, you only need to change the BUILD_LEVEL variable.
Combine this feature with Git/Github/Gitlab workflow, Git hook, webpack, CI/CD tool, and you have a very flexible solution.

Where can i find the link to my newly created app in firebase?

Im trying to locate the link that is supposed to be created along with my app on Firebase(according to the udacity Firebase course).
I found many links but none of them were accepted by the "after lesson quiz" on udacity.
The content that they have is according to the old UI of Firebase so i cant follow it step by step.
Unfortunately that course is for Firebase 2.x (www.firebase.com) and is out of date. In the legacy Firebase docs, under step 5 : Read & Write to your Firebase Database, you can see that creating a Firebase reference required the actual web address for you app. This is no longer true.
The new and correct way to setup your Firebase app does not require you to use this web address. The steps for setting up a Firebase app now are HERE.

Resources