简体   繁体   中英

How to search from the cloud-firestore database?

I want users to type their email and password. After Authentication, based on their email I want to check whether they are admin or not and open different activities thereafter. How should I perform the search query? Is it even possible? 数据库

Below is the answer which worked for me. For general see Alex answer.

mAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    rootRef.collection("Users").whereEqualTo("Email","ashish@gmail.com").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            if (task.isSuccessful()) {
                                for (DocumentSnapshot document : task.getResult()) {
                                    if (document.getString("Admin").equals("Yes")) {
                                        Toast.makeText(Login.this, "Logged In!", Toast.LENGTH_LONG).show();
                                        finish();
                                        startActivity(new Intent(Login.this, MainActivity.class));
                                    } else {
                                        Toast.makeText(Login.this, "Logged In!", Toast.LENGTH_LONG).show();
                                        finish();
                                        startActivity(new Intent(Login.this, nonadmin.class));
                                    }
                                }
                            } else {
                                mProgressBar.setVisibility(View.GONE);
                                Toast.makeText(Login.this, "Sign In Problem", Toast.LENGTH_LONG).show();
                            }
                        }
                    });

                }
            }
        });

To solve this, please use the following code:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
rootRef.collection("Users").whereEqualTo("Email", "ashish@startup.com").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (DocumentSnapshot document : task.getResult()) {
                if (document.getString("Admin").equals("Yes")) {
                    Log.d(TAG, "User is Admin!");
                }
            }
        } else {
            Log.d(TAG, "Error getting documents: ", task.getException());
        }
    }
});

The output will be: User is Admin! .

Don't also forget to set your security rules like this:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM