I am building an Express.js app, using the Firebase Admin SDK for several features such as ID Token validation and Cloud Firestore access. In my main app.js file, I am initializing the app as:
const admin = require('firebase-admin')
const serviceAccount = require('../config/account-credentials.json')
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://databaseurl.firebaseio.com'
})
In another file, all I'm doing is importing:
const admin = require('firebase-admin')
I am able to call admin.auth().verifyIdToken
and verify ID tokens just fine. However when I call app.database()
, it complains that the app is never initialized. Inititalizing the app again creates a new error saying:
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.
Do I need to create multiple apps with different names for this to work? Or how can I use one app throughout the project.