Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initializeApp when adding firebase to app and to server

I'm using firebase for auth and db, and AWS lambda for cloud functions.

To add firebase to my JS project, I initializeApp with the firebase config as parameter, as documented here : https://firebase.google.com/docs/web/setup.

As documented here : https://firebase.google.com/docs/admin/setup, I also need to initializeApp in my lambda function.

Something as follows here :

const admin = require('firebase-admin');
const serviceAccount = require('../path/to/service-account.json');

const firebaseAdmin = admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "dB_URL"
});

The credentials come from the firebase-admin library, so I cannot add this to my web firebase config. So, I need to initialize twice.

However, if I proceed like this, the server will throw an error :

The default Firebase app already exists. This means you called initializeApp() more than once without providing an app name as the second argument. In most cases you only need to call initializeApp() once.But if you do want to initialize multiple apps, pass a second argument to initializeApp() to give each app a unique name.

Am I missing something here ? What's the best practice ? I'm confused.

Someone faced the same issue before it seems : Use Firebase SDK with Netlify Lambda Functions

What worked for this user was to use the REST API as documented here : https://firebase.google.com/docs/projects/api/workflow_set-up-and-manage-project

The documentation says it's in beta though.

Thanks for your kind help

like image 512
John Doe Avatar asked Sep 03 '19 01:09

John Doe


People also ask

What does initializeApp do in Firebase?

const app = initializeApp(firebaseConfig); A Firebase App is a container-like object that stores common configuration and shares authentication across Firebase services. After you initialize a Firebase App object in your code, you can add and start using Firebase services.

How do I connect my existing app to Firebase?

Open the Firebase Assistant: Tools > Firebase. In the Assistant pane, choose a Firebase product to add to your app. Expand its section, then click the tutorial link (for example, Analytics > Log an Analytics event). Click Connect to Firebase to connect your Android project with Firebase.

Can two apps use the same Firebase database?

Yes, You can use the same firebase database in more than one android application as below: In the Project Overview section of Firebase Console add an android application. For adding this application first you need to give that package name.


1 Answers

It seems that lambda may load the script file that calls admin.initializeApp({...}) multiple times. To prevent the Admin SDK from initializing multiple times, you can for example detect if that already happened:

if (!admin.apps.length) {
    const firebaseAdmin = admin.initializeApp({
        credential: admin.credential.cert(serviceAccount),
        databaseURL: "dB_URL"
    });
}
like image 52
Frank van Puffelen Avatar answered Dec 13 '22 03:12

Frank van Puffelen