Firebase Firestore how to list authorized documents?

Viewed 315

I'm trying to create a list of documents. Let's say I have this kind of structure:

cards
  cardId1
    authorizedUser = someuserid1
  cardId1b
    authorizedUser = someuserid1
 cardId2
    authorizedUser = someuserid2

I want to list the first 2 documents for "someuserid1" and the last one for "someuserid2". What's the best way to do it ? I tried using security rules but it didn't work :

service cloud.firestore {
  match /databases/{database}/documents {
    match /cards/{cardid} {
      allow read, write: if resource.data.authorizedUser == request.auth.uid;
    }
  }
}

And this my Kotlin code:

val query = FirebaseFirestore.getInstance()
                    .collection("cards")
                    .limit(50)
val options = FirestoreRecyclerOptions.Builder<Card>()
                .setQuery(query,Card::class.java)
                .build()

adapter = CardFireAdapter(options)

Any idea on what I'm missing here ?

1 Answers

The security rules define what type of query is allowed.

The actual filtering still must be done in your query:

val query = FirebaseFirestore.getInstance()
                .collection("cards")
                .whereField("authorizedUser", isEqualTo: "someuserid1")
                .limit(50)
Related