Mapping Firebase Auth users to Firestore Documents

半世苍凉 提交于 2019-12-03 18:34:29

问题


I'm trying to use Firebase's Firestore database to handle Users in my Android app.

Initially I want to use the id returned from the Auth package (a string), and set that as the id for the users Collection in the database. When creating a Document inside of the Collection users, I set a uid field to that auth package string.

I realize there is no way of setting that string to an indexable value so that queries like:

// get Auth package uid string
db.collection("users").document(auth_uid_string);

would work.

So the alternative, I guess, is to use Query.whereEqualTo("uid", auth_uid_string) which would give me the user in a list, because firestore doesn't assume the query is for a unique value.

I want to avoid the above solution, and devise a way to store the users of the app in firestore, and use the auth package to verify that I fetch the correct user. Is such a solution possible? Or should I try a different firebase service, or even just run a postgres server in heroku or something?


回答1:


I am not sure how to do this in android, but with JS I am doing the following:

firebase.firestore().collection('users').doc(currentUser.uid).set(currentUser)

currentUser is the user object that the authentication function returns (I use signInWithCredential) It is working perfectly. I am sure you can implement a similar approach in android.



来源:https://stackoverflow.com/questions/46622291/mapping-firebase-auth-users-to-firestore-documents

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