(Solution) Multiple Firebase Realtime Database Permission error
Multiple database in the web
You came here because you may realise the example codes in firebase’s documentation may not work for you.
I did not test all the scenarios, however, you may face permission error if you were to use a single instance of Firebase Authentication, and followed the codes in the Firebase documentation.
If you use the following codes, and you have a rule set to allow only authenticated users to read/write…..
const app1 = firebase.initializeApp({
databaseURL: "https://testapp-1234-1.firebaseio.com"
});
const app2 = firebase.initializeApp({
databaseURL: "https://testapp-1234-2.firebaseio.com"
}, 'app2');
// Get the default database instance for an app1
var database1 = firebase.database();
// Get a database instance for app2
var database2 = firebase.database(app2);

You may encounter the following error when you connect to your second real time database.

You can see the above log when you enable Firebase logging
firebase.database.enableLogging(function(message) {console.log(“[FIREBASE]”, message);});
This is likely caused by you creating 2 separate instances of Firebase Realtime database when you did firebase.initializeApp twice (if you followed the documentations).
To resolve the permission error, and to allow the rule to be applied, you have to initializeApp only once with the sample code like below.
const app = firebase.initializeApp({ databaseURL: /*Realtime DB1 URL*/});const db = firebase.database() //this is how you usually initialize default db. const db2 = app.database(/* Realtime DB2 URL */) //you should initialize the db from your app instance
This way, your authentication session will be properly passed to your rules and you can apply the rules check with auth.uid