Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FireStore: query the Firebase Auth table?

I'm building an app using Firestore, where a group of people need to be able to access an object, and I followed the guide at https://firebase.google.com/docs/firestore/solutions/role-based-access. So, my object looks a little bit like this:

{
  title: "A Great Story",
  content: "Once upon a time ...",
  members: {
    alice: "host",
    bob: "player",
    david: "player",
    jane: "player"
  }
}

Those keys in members are simply User UID's, so in reality they don't look quite as nice of course :)

Now, this all works fine, I can query objects that the currently logged in user is a member of, all the rules are working and stuff. But, now I want to show the list of members in my app. How can I query the Firebase Auth system for the users in my members map?

And if that is not possible, what is the "normal" way to solve this? Have a copy of the user information (name, email, profile picture) in the document itself? But how do you handle a user changing any of own info? Then the document has stale old data..

I'm coming from a traditional relational database world, these are my first steps with a NoSQL database / Firestore, so it's not really clear to me how to best structure my user data and keep it up to date.

like image 735
Kevin Renskers Avatar asked Oct 26 '25 08:10

Kevin Renskers


1 Answers

With the client SDKs there is no way to list all the users of your Firebase project.

On the other hand, the Admin SDK allows retrieving the entire list of users in batches, see https://firebase.google.com/docs/auth/admin/manage-users#list_all_users. Note that if you want to use this possibility offered by the Admin SDK, you need to do it from a "privileged environment" (see https://firebase.google.com/docs/admin/setup), for example with a Cloud Function. You would then need, from you front-end, to call this Cloud Function.

However, one of the classical approach is to maintain a list of all your users in a dedicated Cloud Firestore collection. There are several advantages with this approach, among which:

  • You can easily query this list with the client SDKs;
  • You can store some extra info in the user profiles document, e.g. the user roles. As a matter of fact, with the Admin SDK's listUsers() method you get the result as UserRecord object which has a fixed list of properties.
like image 96
Renaud Tarnec Avatar answered Oct 28 '25 22:10

Renaud Tarnec