1

I am a beginner to Android and got stuck at a problem stating "Firebase permission denied error". Following is my code snippet:

private void searchUser() {
        Log.d(Constants.TAG, "GetUser:  userId " + mFirebaseUser.getUid()+"   mFirebaseUser.getPhoneNumber()  " +mFirebaseUser.getPhoneNumber());

        if (!TextUtils.isEmpty(mFirebaseUser.getUid())) {
            showSnackBar(getString(R.string.fetching_profile_details), false);
            Utils.showProgressDialog(mActivity, false);

            final FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference userRef = database.getReference(Constants.FIREBASE_USERS);
            Query query = userRef.orderByChild(Constants.FIREBASE_USER_PHONE)
                    .equalTo(mFirebaseUser.getPhoneNumber())
                    .limitToFirst(1);

           query.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Utils.hideProgressDialog();
                    if (dataSnapshot != null && dataSnapshot.exists() && dataSnapshot.getChildrenCount() > 0) {
                        //Found user with same number
                        //Log.d(Constants.TAG,"dataSnapshot  " +dataSnapshot);

                        for(DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
                            //Log.d(Constants.TAG, "childDataSnapshot  " + childDataSnapshot);
                            mUser = childDataSnapshot.getValue(com.hypertrack.wintry_consumer.models.User.class);
                        }

                        if (mUser != null) {
                            isNewUser = true;
                            preFillData();
                        }
                    }
                }

                @Override
               public void onCancelled(DatabaseError databaseError) {
                    Utils.hideProgressDialog();
                    Log.e(Constants.TAG, "onCancelled", databaseError.toException());
                    showSnackBar("Firebase Error finding User details: " + databaseError.getMessage());
                }
            });
        } else {
            showSnackBar("No firebase user available ");
        }
    }

Following is my cloud firestore rule:-

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

I am getting the following error:

"Firebase Database Error: Permission denied"

Android Studio Screenshot

Please help with its solution.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Abhi
  • 614
  • 2
  • 12
  • 26

1 Answers1

4

You are showing us how to query data from a Firebase realtime database but you added the security rules from Cloud Firestore. In order to be able to query the Firebase realtime database you should also change the rules in the specific section accordingly. If you set the correct rules in Cloud Firestore it doesn't mean that it will also work in Firebase realtime database. There are two separate products with different rules.

To solve this, open your Firebase Console, select your project, click on the database section on the left hand side and and choose Realtime Database then go to the Rules tab and make sure you have the following persmissions:

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

Make sure to visit official documentation regarding security rules.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193