Firebase实时数据库权限被拒绝

时间:2020-05-11 07:39:36

标签: java android firebase-realtime-database firebase-security

我正在尝试将数据写入数据库。我将规则设置如下-

{
"rules": {
 "users": {
   "$uid": {
     ".read" : "$uid === auth.uid",
     ".write": "$uid === auth.uid"
    }
  }
 }
}

但是,我继续收到权限被拒绝错误。成功通过身份验证后,将调用createRecordForUserInDatabase(...)方法,并且我还可以使用Log.i("USER", mAuth.getCurrentUser().getUid())在日志中记录用户UID。 在数据库中写入设置值的代码如下-

private void createRecordForUserInDatabase(final String userFullName) {
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference reference = database.getReference("users").child(mAuth.getCurrentUser().getUid());
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(!dataSnapshot.exists()){
                FirebaseDatabase rootnode = FirebaseDatabase.getInstance();
                DatabaseReference databaseReference = rootnode.getReference("users");
                UserHelperClass user = new UserHelperClass(userFullName);
                databaseReference.child(mAuth.getCurrentUser().getUid()).setValue(user)
                        .addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {
                                startActivity(new Intent(getApplicationContext(),Profile.class));
                                finish();
                            }
                        });
            } else {
                Toast.makeText(LoginActivity.this, "Full Name Exist", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(getApplicationContext(),Profile.class));
                finish();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

我不明白为什么这段代码不起作用。请帮帮我。

0 个答案:

没有答案