目前,我希望在保留用户身份验证的同时向数据库添加对象,当我将Firebase中的规则设置为public时,它可以完美地运行。但是,当我将它们设置为用户Auth时,即使用户已在上一页登录,我也会收到权限被拒绝的问题。我如何克服这个问题,因为我需要为我的应用程序验证。
主:
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": {
"users": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
}
}
}
答案 0 :(得分:0)
确保您已关联Firebase配置.JSON文件,其中包含您的API密钥以及您帐户的其他一些有用信息。