[Development] Using two or more Firebase projects in one app

in upvudevkr •  2 years ago 

Hello @amitmusaddi

To separate Firebase projects and use the separate project's authentication system in Node.js, you must initialize the Firebase Admin SDK by importing each Firebase project's authentication information and authentication system.

Here is example code for a Node.js application that uses the two Firebase projects' authentication systems:

const admin = require('firebase-admin');

// First Firebase Project Credentials
const firstServiceAccount = require('/path/to/firstServiceAccountKey.json');
admin.initializeApp({
credential: admin.credential.cert(firstServiceAccount),
databaseURL: 'https://<FIRST_DATABASE_NAME>.firebaseio.com'
});
const firstAuth = admin.auth();

// second Firebase project credentials
const secondServiceAccount = require('/path/to/secondServiceAccountKey.json');
const secondApp = admin.initializeApp({
credential: admin.credential.cert(secondServiceAccount),
databaseURL: 'https://<SECOND_DATABASE_NAME>.firebaseio.com'
}, 'secondApp');
const secondAuth = secondApp.auth();

// Example of using the first Firebase project authentication system
firstAuth.getUserByEmail('[email protected]')
.then(userRecord => {
console.log(userRecord.toJSON());
})
.catch(error => {
console. log(error);
});

// Examples of using the authentication system for the second Firebase project
secondAuth.getUserByEmail('[email protected]')
.then(userRecord => {
console.log(userRecord.toJSON());
})
.catch(error => {
console. log(error);
});
The code above uses the firebase-admin module to initialize the auth system of the two Firebase projects, and imports the auth() method of each using the auth() method. After that, I wrote example code to retrieve user information using each authentication system.

In the code above, we wrote an example code to retrieve user information via email using the getUserByEmail method, but the firebase-admin module provides various methods related to authentication, so you can find and use the method you need.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!