首页 > 解决方案 > 如果 request.auth != null 是在 mAuth.createUserWithEmailAndPassword(email, password) 的 onComplete 中创建文档时的唯一规则,则权限被拒绝

问题描述

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

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() 方法成功,则创建我们的默认文档等。

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

有任何想法吗?

标签: androidgoogle-cloud-firestorefirebase-security

解决方案


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

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

推荐阅读