0

Currently I want to add objects to a database while retaining user authentications, when I set the rules in the Firebase to public it works perfectly. But when I set them to user Auth I get permission denied problem, even though a user is signed in on the previous page. How do I overcome this problem as I require auth for my app.

Main:

public class CreateActivity extends AppCompatActivity implements View.OnClickListener{

Button buttonSave;
Button buttonAdd;
EditText addName;
EditText addItems;
ListView showItems;

ArrayList<String> items = new ArrayList<String>();

private String userID;
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;


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


    mAuth = FirebaseAuth.getInstance();
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    myRef = mFirebaseDatabase.getReference();
    FirebaseUser user = mAuth.getCurrentUser();
    userID = user.getUid();

    if(mAuth.getCurrentUser() == null){
        finish();
        startActivity(new Intent(this, MainActivity.class));
    }


    buttonSave = (Button)findViewById(R.id.buttonSave);
    buttonAdd = (Button)findViewById(R.id.buttonAdd);
    addName = (EditText)findViewById(R.id.etProfileName);
    addItems = (EditText)findViewById(R.id.etItems);
    showItems = (ListView)findViewById(R.id.lvItems);

    buttonAdd.setOnClickListener(this);
    buttonSave.setOnClickListener(this);

}

@Override
public void onClick(View view) {

    String getInput = addItems.getText().toString();

    if (view == buttonAdd) {
        if (items.contains(getInput)) {
            Toast.makeText(getBaseContext(), "Item has already been Added", Toast.LENGTH_SHORT).show();
        } else if (getInput == null || getInput.trim().equals("")) {
            Toast.makeText(getBaseContext(), "Input field cannot be Empty", Toast.LENGTH_SHORT).show();
        } else {
            items.add(getInput);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(CreateActivity.this, android.R.layout.simple_list_item_1, items);
            showItems.setAdapter(adapter);
            ((EditText) findViewById(R.id.etItems)).setText(" ");
        }
    }
    if (view == buttonSave) {
        String key = addName.getText().toString();
        if (!key.equals("")) {
            myRef.child(userID).child(key).setValue(items);
        }
        else {
            Toast.makeText(getBaseContext(), "Name field cannot be Empty", Toast.LENGTH_SHORT).show();
        }
    }
}

@Override
public void onBackPressed() {
    finish();
}



}

rules:

{
  "rules": {
    "users": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      }
    }
  }
}
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Kyril Khaltesky
  • 85
  • 1
  • 1
  • 7

1 Answers1

0

Make Sure you have linked Firebase configuration .JSON file, Which Contains your API Key and Some other useful information for your account.

check out this :- https://support.google.com/firebase/answer/7015592