1

Let me explain the situation (excuse my English, I will do my best):

I have two Firebase Web projects in my Firebase console: coretechtest-ce207 and agon-plugin

coretechtest-ce207 is the main app and agon-plugin is a secondary app which needs to connect to the auth and database of coretechtest-ce207. As far as I know I can't host two apps on the same project so that's why I made to separated projects. The main one works fine, I can do everything I want (signup, database, etc.) but I need the main and the second one both on the same auth and DB. agon-plugin (secondary one) is made based on the FriendlyChat app and connects directly to the server in which the app is hosted.

For example:

// Initializes FriendlyChat.
function FriendlyChat() {
    this.initFirebase();

}

// Sets up shortcuts to Firebase features and initiate firebase auth.
FriendlyChat.prototype.initFirebase = async function() {
    // Shortcuts to Firebase SDK features.
    this.auth = firebase.auth();
    this.database = firebase.database();
    this.storage = firebase.storage();
    // Initiates Firebase auth and listen to auth state changes.
    await this.auth.onAuthStateChanged(this.onAuthStateChanged.bind(this));   
};

As you can see there is no need to put

apiKey: "AIzaSyAfGm_ILVdfsd--Fw7aascc8tAB73q__Bbko",
authDomain: "coretechtest-ce207.firebaseapp.com",
databaseURL: "https://coretechtest-ce207.firebaseio.com",
projectId: "coretechtest-ce207",
storageBucket: "coretechtest-ce207.appspot.com",
messagingSenderId: "994718782"

I tried replacing it using those parameters so it would be

    FriendlyChat.prototype.initFirebase = async function() {
// Shortcuts to Firebase SDK features.

    this.auth = 'coretechtest-ce207.firebaseapp.com';
    this.database = 'https://coretechtest-ce207.firebaseio.com';
    this.storage = 'coretechtest-ce207.appspot.com';

// Initiates Firebase auth and listen to auth state changes.
   await this.auth.onAuthStateChanged(this.onAuthStateChanged.bind(this));   
};

But no luck there, can you tell me what I am doing wrong? I thought that replacing that would connect to my main project but it did not...

I hope you can understand what I'm trying to say!

Thanks!

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Ace
  • 469
  • 1
  • 7
  • 17

2 Answers2

1

Not 100% sure about Web, but it should be similar to Android :

  • Going to your console panel
  • On top left click on the wheel next to "Project OverView"
  • In the pop-up click on Project Setting
  • In the settings page scroll down a little bit and you should see a blue "add app" (or something like that) button.

Then do everything you did on your first app to your second app

Most importantly, dont post your api key on the web !

Davide Valiante
  • 155
  • 2
  • 12
  • Hi! first thanks! Dont worry, thats not my api key ;) Im gonna try what you said! – Ace Dec 02 '17 at 00:09
  • Since we're bike shedding on the API key already: https://stackoverflow.com/questions/37482366/is-it-safe-to-expose-firebase-apikey-to-the-public :-) – Frank van Puffelen Dec 02 '17 at 01:54
0

If you're using Web and want to host 2 webapps that use the same database - you have a couple options. Free: the only thing that associates a web app to your db is the api keys in the Firebase config used to initialize firebase in the web app. If 2 apps use those same settings then they can share the same db. If you are wanting to host both web apps with firebase for free - only 1 app can be hosted for free per project. However, you can easily create a 2nd new project and host your 2nd web app in that new project. But don't use the new web api settings from that new project inside your app. Instead use the same ones from your original project. 2 apps in 2 projects can use the same DB if they use the same API keys config. (only select firebase hosting when doing 'firebase init' from the cli for the 2nd app)

A paid option: is if you go to the Hosting page in your firebase console, and you scroll down to the bottom -you will see a card offering hosting more than 1 app in 1 project. It requires you to update to the pay-as-you-go blaze plan. Which may or may not cost you money.

SeanMC
  • 1,960
  • 1
  • 22
  • 33