首页 > 解决方案 > 权限被拒绝:在 Android 上缺少权限或权限不足,但适用于 ios Firestore

问题描述

我正在尝试在 android 设备的 firestore 中更新我的数据库,但收到错误“缺少权限或权限不足”。如果这是由于规则造成的,那么相同的规则适用于 iOS 中的更新,但不适用于 android。有人可以指出我正确的规则。以下是我正在使用的规则

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

    // This rule allows anyone on the internet to view, edit, and delete
    // all data in your Firestore database. It is useful for getting
    // started, but it is configured to expire after 30 days because it
    // leaves your app open to attackers. At that time, all client
    // requests to your Firestore database will be denied.
    //
    // Make sure to write security rules for your app before that time, or else
    // your app will lose access to your Firestore database


    match /users/{userId} {
      allow read, update, delete: if request.auth != null;
      allow create: if request.auth != null;
    }
  }
}

我用来更新记录的代码

 db.collection("user").document(documentSnaapShotId).update(userData).addOnSuccessListener(new OnSuccessListener<Void>() {
           @Override
           public void onSuccess(Void aVoid) {
               sendUserToInitialPage();
           }
       }).addOnFailureListener(new OnFailureListener() {
           @Override
           public void onFailure(@NonNull Exception e) {
               String error = e.getMessage();
               Toast.makeText(UpdateUserActivity.this, "Error:" + error, Toast.LENGTH_LONG).show();
           }
       });

此图像是我用来验证用户身份的代码,只有当用户通过电话进行身份验证时,代码特定活动才会打开。现在,在调试时,我发现了另一个问题,当我单击注销 (FirebaseAuth.getInstance().signout) 时,它会返回此页面并引发相同的错误,并且应用程序会自行终止。我猜我验证用户的方式存在一些问题。请提供任何帮助。下面是我用于验证用户的整个代码

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            mFirestore = FirebaseFirestore.getInstance();
                            String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
                            mFirestore.collection("mydbexample").whereEqualTo("uid", uid).addSnapshotListener(new EventListener<QuerySnapshot>() {
                                @Override
                                public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                                    if(e != null) {
                                        Toast.makeText(PhoneAuthActivity.this, "Error:" + e, Toast.LENGTH_LONG).show();
                                    }

                                    for(DocumentSnapshot doc : queryDocumentSnapshots) {
                                        docId = doc.getId();
                                        userFname = doc.getString("firstname");
                                        userLname = doc.getString("lastname");
                                        userCountry = doc.getString("country");
                                        userState = doc.getString("state");
                                        userCity = doc.getString("city");
                                        userArea = doc.getString("area");


                                        documentExists = (userFname!= null);

                                    }

                                    if(documentExists){

                                        sendUserToInitialPage();

                                    }

                                    else {

                                        sendUserToHome();
                                    }
                                }
                            });


                        } else {
                            if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                                Toast.makeText(getApplicationContext(),
                                        "Incorrect Verification Code ", Toast.LENGTH_LONG).show();
                            }
                        }
                    }
                });
    }

private void sendUserToHome() {
        Intent homeIntent = new Intent(PhoneAuthActivity.this, GuestUserActivity.class);
        homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        homeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        startActivity(homeIntent);
        finish();

    }

    private void sendUserToInitialPage() {
        Intent homeIntent = new Intent(PhoneAuthActivity.this, InitialPageActivity.class);

        homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        homeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

        startActivity(homeIntent);
        finish();

    }

调用堆栈

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.app.virtualbloodbank, PID: 489
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Iterator com.google.firebase.firestore.QuerySnapshot.iterator()' on a null object reference
        at com.app.virtualbloodbank.PhoneAuthActivity$4$1.onEvent(PhoneAuthActivity.java:224)
        at com.app.virtualbloodbank.PhoneAuthActivity$4$1.onEvent(PhoneAuthActivity.java:217)
        at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal$2(com.google.firebase:firebase-firestore@@21.4.0:1039)
        at com.google.firebase.firestore.Query$$Lambda$3.onEvent(Unknown Source:6)
        at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0(com.google.firebase:firebase-firestore@@21.4.0:42)
        at com.google.firebase.firestore.core.AsyncEventListener$$Lambda$1.run(Unknown Source:6)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

标签: androidiosgoogle-cloud-firestorefirebase-security

解决方案


推荐阅读