将 Firebase Auth 用户映射到 Firestore 文档 [英] Mapping Firebase Auth users to Firestore Documents
问题描述
我正在尝试使用 Firebase 的 Firestore
数据库来处理我的 Android 应用中的User
.
I'm trying to use Firebase's Firestore
database to handle User
s in my Android app.
最初我想使用从 Auth
包(一个字符串)返回的 id,并将其设置为 users
的 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 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!