title: Permission Denied: Missing or insufficient permissions on Android but working for ios Firestore
body: <p>I am trying to update my db in firestore in an android device but receiving error "Missing or insufficient permissions". If this is due to the rules, the same rule is working fine for update in iOS but not in android. Can someone point me out to a right rule. Below is the rule that i am using</p>
  
  <pre><code>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;
      }
    }
  }
  </code></pre>
  
  <p>Code i am using to update the record</p>
  
  <pre><code> db.collection("user").document(documentSnaapShotId).update(userData).addOnSuccessListener(new OnSuccessListener&lt;Void&gt;() {
             @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();
             }
         });
  
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/2ksfq.png" rel="nofollow noreferrer">This image</a> is the code that i used to authenticate user and the code specific activity opens only if the user has been authenticated via phone. Now while debugging i found another issue where when i clicked on signout (FirebaseAuth.getInstance().signout) it comes back to this page and throws the same error and app kills itself. I am guessing there is some issue with the way i am authenticating user. Any help please. Below is the whole code that i used for authenticating the user</p>
  
  <pre><code>private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
          mAuth.signInWithCredential(credential)
                  .addOnCompleteListener(this, new OnCompleteListener&lt;AuthResult&gt;() {
                      @Override
                      public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
                          if (task.isSuccessful()) {
                              mFirestore = FirebaseFirestore.getInstance();
                              String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
                              mFirestore.collection("mydbexample").whereEqualTo("uid", uid).addSnapshotListener(new EventListener&lt;QuerySnapshot&gt;() {
                                  @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();
  
      }
  </code></pre>
  
  <p>Call Stack </p>
  
  <pre><code>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)
  </code></pre>