-1

I am trying to integrate Firebase in my Android application.I have followed the steps as documented by the fire base. When I am trying to send data, I am not able to send. I am getting below error in callback

**Permission denied**

And the error code in callback is -3

I have searched relative answers over stack overflow in many places and it did not work for me. As this is the very famous thread

Firebase Permission Denied

In this thread it is mentioned that I need to add below code in database section where the rules are defined. And below is the code

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

When I went to the place as mentioned in that thread (went to database section and then rules area) I already found some piece of code. And that code is mentioned below

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

My Problem is I am getting permission denied error when commnuicating the database. When I am adding code as mentioned in the thread, Fire base is not recognizing that code. What is the right place to place code if there is a need? Or why am I getting that error? Am I missing something.

Below is the code I am using for firebase communication

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_signup);

    mDatabaseReference = FirebaseDatabase.getInstance().getReference();
    DatabaseReference databaseReference = mDatabaseReference.child("users");

    //Get Firebase auth instance
    //auth = FirebaseAuth.getInstance();


    btnSignIn = findViewById(R.id.sign_in_button);
    btnSignUp = findViewById(R.id.sign_up_button);
    inputEmail = findViewById(R.id.email);
    inputPassword = findViewById(R.id.password);
    progressBar = findViewById(R.id.progressBar);
    btnResetPassword = findViewById(R.id.btn_reset_password);

    btnResetPassword.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //startActivity(new Intent(SignupActivity.this, ResetPasswordActivity.class));
        }
    });

    btnSignIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    btnSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String email = inputEmail.getText().toString().trim();
            String password = inputPassword.getText().toString().trim();

            if (TextUtils.isEmpty(email)) {
                Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
                return;
            }

            if (TextUtils.isEmpty(password)) {
                Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
                return;
            }

            if (password.length() < 6) {
                Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
                return;
            }

            progressBar.setVisibility(View.VISIBLE);

            User user = new User();
            user.setEmail(email);
            user.setPassword(password);
            user.setUserType(1);

            mDatabaseReference.child("users").setValue(user, new DatabaseReference.CompletionListener() {
                @Override
                public void onComplete(@Nullable DatabaseError databaseError, @NonNull DatabaseReference databaseReference) {

                    progressBar.setVisibility(View.GONE);

                    databaseReference.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            dataSnapshot.exists();
                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {
                            databaseError.getCode();
                        }
                    });

                }
            });


        }
    });
}

NOTE: setValue at /users failed: DatabaseError: Permission denied this is log I am getting.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58

1 Answers1

2

Your rules say only authorized users can read and write from database but in your code you are trying to insert data with unregistered user

1- Either register user with firebase before inserting data

or

2- Allow unauthorized users also to write in database

Rule:-

{
  "rules": {

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

enter image description here

Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
Manohar
  • 22,116
  • 9
  • 108
  • 144