我目前正在编写一个 Android 应用程序。在我的应用程序中,用户可以对某些对象进行评分。数据(对象以及评级)应存储在 Google Firebase,尤其是 Firestore 中。
我已经将我的应用链接到 Firebase 并实现了 com.google.android.gms.auth.api.signin
以获取当前用户的 GoogleSignInAccount
。
我想将数据保存到数据库中,例如:
SomeObject: | objectId | name |
ObjectRating: | ratingId | objectId | userId | rating |
我不确定的是,将什么保存为 userId。 GoogleSignInAccount.getId()
的文档说:
Important: Do not use this returned Google ID to communicate the currently signed in user to your backend server. Instead, send an ID token (requestIdToken(String)), which can be securely validated on the server; or send a server auth code (requestServerAuthCode(String)) which can be in turn exchanged for id token.
所以我可能不应该使用这个。但推荐的IdToken
是有签名的,所以我认为每次调用该方法都会不同。
大多数人所做的就是在数据库中创建一个用户目录。但我想从应用程序中获取用户评分以及所有用户对给定对象的总体平均评分。因此,我认为将评分拆分到多个用户表中会使整体平均评分的计算变得复杂。
我的问题是:如何在不使用不安全的 getId()
的情况下将 ObjectRating 链接到用户,同时仍然能够通过 objectId 选择所有 ObjectRating 来计算平均评分?
最佳答案
要获取用户的唯一标识符,您可以使用 FirebaseAuth
获取 FirebaseUser
对象:
FirebaseAuth.getInstance().getCurrentUser().getUid();
getUid()
方法:
Returns a string used to uniquely identify your user in your Firebase project's user database. Use it when storing information in Firebase Database or Storage, or even in your own backend.
您需要确保您已used the GoogleSignInAccount
to authenticate with Firebase调用getCurrentUser()
之前,否则将返回null
。
关于android - Firebase 保存用户相关数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46727967/