仅当在mAuth.createUserWithEmailAndPassword(电子邮件,密码)的onComplete中创建文档时,如果request.auth!= null是仅规则,则拒绝权限

时间:2019-04-29 19:30:38

标签: android google-cloud-firestore firebase-security-rules

如果我的“安全规则”中有以下规则(唯一规则):

match /{document=**} {
      allow read, write: if request.auth != null;
    }

然后执行以下操作:

mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Log.d(TAG, "createUserWithEmail:success");
                        //initial database setup
                        final WriteBatch adminSetupBatch = mDbase.batch();

                        //create adminSUsersGroup and add documentID value as a field in document
                        DocumentReference adminGroupRef = mDbase.collection("adminUserGroups").document();

                        Map<String, Object> adminGroup = new HashMap<>();
                        adminGroup.put("adminID", task.getResult().getUser().getUid());
                        adminGroup.put("age", -1);
                        adminSetupBatch.set(adminGroupRef, adminGroup);

                        //Commit the batch
                        adminSetupBatch.commit().addOnCompleteListener(new OnCompleteListener<Void>() {

                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                         if (task.isSuccessful()) {
                               Log.d(TAG, "Admin setup batch write completed");
                              Intent intent = new Intent(activity, MainActivity.class);
                              activity.startActivity(intent);
                              Toast.makeText(MyApplication.getContext(), MyApplication.getContext().getString(R.string.alert_adminAccountCreated),
                                       Toast.LENGTH_SHORT).show();
                         }
                            else {
                             Log.d(TAG, "Error: completing Admin setup batch write", task.getException());
                             Toast.makeText(MyApplication.getContext(), task.getException().getMessage(),
                                        Toast.LENGTH_SHORT).show();
                            }
                        }

             });

                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "createUserWithEmail:failure", task.getException());
                        Toast.makeText(MyApplication.getContext(), task.getException().getMessage(),
                                Toast.LENGTH_SHORT).show();
                    }
                }
            });

如果createUserWithEmailAndPassword()方法成功,则创建我们的默认文档等。

但是由于某种原因,我收到一条权限被拒绝的消息,并且未创建任何文档。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

好的,没关系,我想我已经解决了这个问题。首先,我正在使用:

if isSignedIn();

在我的生产代码安全规则中,该规则调用以下函数:

function isSignedIn() {
   return request.auth != null;
}

由于我正在测试并在不同的规则之间来回移动,因此我显然忘记了也添加了调用函数。因此,从本质上讲,规则总是失败的。

答案 1 :(得分:0)

仅尝试将您在firebase中的权限更改为

match /{document=**} {
  allow read, write;
}