bakua
bakua

Reputation: 14454

Firebase DatabaseError(-3, Permission denied,) when accessing via app copy

I am trying to get around Firebase persistence, clear Firebase cache on Flutter.

TLDR the issue is that I don't want to listen on data changes through the app but I just want to download the data snapshot on start. But when you have persistence enabled Firebase Realtime Database serves previously cached data first and updates in background. Meaning you won't get a fresh data when you ask for it unless you ask multiple times.

My workaround is to create a new, throwaway Firebase app instance for fetching snapshot that doesn't use presistence.

  final defaultOptions = await FirebaseApp.instance.options;
  final appName = 'second';
  final app = await FirebaseApp.configure(name: appName, options: defaultOptions);
  final fdb = FirebaseDatabase(app: app, databaseURL: defaultOptions.databaseURL);

However, when I try to access a node via fdb I get Firebase DatabaseError(-3, Permission denied,). Accessing the database via default app instance works just fine, but I need that one to have persistence enabled, for data uploads.

I am not sure how to achieve what I need to do now.

Edit:

My query:

final firebaseTags = fdb
      .reference() //
      .child('tags')
      .child(user.id)
      .orderByChild('synchronized_at')
      .startAt(syncFrom + 1, key: 'synchronized_at')
      .once();

Upvotes: 0

Views: 143

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317322

Each Firebase app operates in complete isolation from each other. That means the user signed into the first app is not at all recognized by the other app. You will need to find a way to sign the user into the second app in order for security rules to work correctly. Unfortunately this is not always possible. If you're not able to get a credential from your authentication provider and use signInWithCredential, you're going to put the user through another signin process.

Upvotes: 1

neohaojun
neohaojun

Reputation: 312

If you are using Cloud Firestore, make sure that your database has the following rules:

allow read, write: if true;

If you are using Realtime Database, make sure that your database has the following rules:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

These 2 rules allow anyone to write and read data in your Firebase Database. However, it is not recommended in production as it allows anyone to overwrite your entire database. It should be fine for testing purposes, though.

If you are using both Cloud Firestore and Realtime Database, make sure that you have set the security rules in both databases correctly as rules in one database do not affect the other.

To switch between Cloud Firestore and Realtime Database, go to the top left-hand corner in the 'Database' section. Choose the database you want accordingly.

Be sure to read the official documentation for security rules for:

The official documentation has more information on how to set up security rules with conditions (safer for production).

Upvotes: 0

Related Questions