FireStore: query the Firebase Auth table?

帅比萌擦擦* 提交于 2020-01-21 15:19:04

问题


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.


回答1:


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.


来源:https://stackoverflow.com/questions/55902940/firestore-query-the-firebase-auth-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!