(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

--

--

--

entrepreneur, developer, dream chaser

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Controlled and Uncontrolled Input Values in React

Working an application in Vue.js with TDD — An extensive guide for people who have time — part 3

How to solve Jest storybook test error “cannot find interopRequireDefault.js”

The Ultimate Guide To JavaScript SEO

Building a tree shaking package using rollup.

.ts is missing from the TypeScript compilation.

JavaScript Architecture: Backbone.js Views

How to change the order of JSON parameters in the request? (Rest Template)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dave Teu

Dave Teu

entrepreneur, developer, dream chaser

More from Medium

Passwordless Email Link Login Using Firebase Auth in Ionic Angular App

Firebase user count per provider

Using 2FA in your Meteor app

Setting Up Firebase Local Emulators using NodeJS