1

I am new to firebase. I want to read/write data with firebase in my app but when I try to do so I am getting this DatabaseError: Permission denied error.

Rules file on firebase

{

"rules": {
    "sos":{
          ".read":"auth != null",
          ".write":"auth != null"
          }
}
}

Code file

        Firebase authFirebaseRef = new Firebase(ApiConstants.FIREBASE_MY_URL);
        authFirebaseRef.authWithCustomToken(AppConstants.FBT, authResultHandler);

Firebase.AuthResultHandler authResultHandler = new Firebase.AuthResultHandler() {
    @Override
    public void onAuthenticated(AuthData authData) {
        Log.v("onAuthenticated", authData.getUid());
        addEvents();
    }

    @Override
    public void onAuthenticationError(FirebaseError firebaseError) {
        Log.v("onAuthenticationError= ", "FirebaseError= " + firebaseError.toString());
    }
};

private void addEvents() {

    mFirebaseDatabase = mFirebaseInstance.getReference("sos").child("emergencyservices");
    mFirebaseDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            emergencyServicesList.clear();
            for (DataSnapshot dataSnapshotChild : dataSnapshot.getChildren()) {
                Log.v("onDataChange", ":" + dataSnapshotChild.child("emergencyServicesName").getValue());
                EmergencyServiceModel emergencyServiceModel = new EmergencyServiceModel();
                emergencyServiceModel.setEmergencyServiceId(dataSnapshotChild.child("emergencyServicesId").getValue().toString());
                emergencyServiceModel.setEmergencyServiceName(dataSnapshotChild.child("emergencyServicesName").getValue().toString());
                emergencyServicesList.add(emergencyServiceModel);
            }
            loadDataToRecyclerView();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.v("DatabaseError", databaseError.getMessage());
        }
    });
}

Earlier I added few records in sos/emergencyservices by keeping .true to read/write. But now I have to change it with authentication.

VVB
  • 7,363
  • 7
  • 49
  • 83

5 Answers5

1

I think you're using different firebase object for auth and a different one for query.
Try this

authFirebaseRef.getRoot().child("sos/emergencyservices"‌​).addValueEventListe‌​ner(new ValueEventListener() { ... }; 

instead of

mFirebaseDatabase = mFirebaseInstance.getReference("sos").child("emergencyservic‌​es");
mFirebaseDatabase.addValueEventListener(new ValueEventListener() { ... };

Also, you are using the old version of firebase API. I refer you this link. The document guides you through upgrading your existing Firebase.com app to the new Firebase console and APIs.

VVB
  • 7,363
  • 7
  • 49
  • 83
Niraj Niroula
  • 2,376
  • 1
  • 17
  • 36
1

We have one more source of the "Permission denied" errors, as of end of 2018.

Google has just introduces Cloud Firestore BETA.

The point is it's interface is the same as for Firebase, there is only one dropdown selector to separate both databases ...

Have spent 8h faiting with error, finally have found the default console view is a Cloud Firestore BETA view but not Firebase.

So my app talked to Firebase while I was configuring Cloud Firestore BETA.

8h lost .... Thank you Google ...

Paul Paku
  • 360
  • 2
  • 16
  • This was my poroblem too, after I configured the rules for the realtime database and not the cloud store. – DMonkey Dec 11 '18 at 13:39
0

You need to ensure the user is signed in before attaching the listener. Since you're already listening for the authentication state, it's a matter of moving the code that attaches the listener into the onAuthenticated callback:

Firebase.AuthResultHandler authResultHandler = new Firebase.AuthResultHandler() {
    @Override
    public void onAuthenticated(AuthData authData) {
        Log.v("onAuthenticated", authData.getUid());
        addEvents();

        mFirebaseDatabase = mFirebaseInstance.getReference("sos").child("emergencyservices");
        mFirebaseDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                emergencyServicesList.clear();
                for (DataSnapshot dataSnapshotChild : dataSnapshot.getChildren()) {
                    Log.v("onDataChange", ":" + dataSnapshotChild.child("emergencyServicesName").getValue());
                    EmergencyServiceModel emergencyServiceModel = new EmergencyServiceModel();
                    emergencyServiceModel.setEmergencyServiceId(dataSnapshotChild.child("emergencyServicesId").getValue().toString());
                    emergencyServiceModel.setEmergencyServiceName(dataSnapshotChild.child("emergencyServicesName").getValue().toString());
                    emergencyServicesList.add(emergencyServiceModel);
                }
                loadDataToRecyclerView();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.v("DatabaseError", databaseError.getMessage());
            }
        });
    }

    @Override
    public void onAuthenticationError(FirebaseError firebaseError) {
        Log.v("onAuthenticationError= ", "FirebaseError= " + firebaseError.toString());
    }
};
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

Well, did you enable the required sign-in method in firebase console?

firebase_signin_method

Niraj Niroula
  • 2,376
  • 1
  • 17
  • 36
-1

It is related to database so in firebase console get in database and set rules to true for read/write

CodeToLife
  • 3,672
  • 2
  • 41
  • 29