问题描述
我正在尝试使用 Firebase 的Firestore
数据库来处理Android应用中的User
.
I'm trying to use Firebase's Firestore
database to handle User
s in my Android app.
最初,我想使用从Auth
包返回的 id (字符串),并将其设置为数据库中users
Collection
的ID.在Collection
users
内部创建Document
时,我将uid
字段设置为该身份验证软件包字符串.
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);
会工作的.
因此,我猜想,替代方法是使用Query.whereEqualTo("uid", auth_uid_string)
,它将为我提供列表中的用户,因为firestore不假设查询是针对唯一值的.
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.
我想避免上述解决方案,而是想出一种方法来将应用程序的users
存储在firestore
中,并使用auth
包来验证我是否提取了正确的用户.这样的解决方案有可能吗?还是我应该尝试使用不同的firebase服务,甚至只在heroku或其他操作系统中运行Postgres服务器?
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?
推荐答案
我不确定如何在android中做到这一点,但是使用JS,我正在执行以下操作:
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
是身份验证功能返回的用户对象(我使用signInWithCredential
)运行良好.我确定您可以在android中实现类似的方法.
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.
这篇关于将Firebase Auth用户映射到Firestore文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!