android - Firebase 保存用户相关数据

标签 android firebase firebase-realtime-database firebase-authentication google-signin

我目前正在编写一个 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/

相关文章:

java - 创建异步任务时出现下划线错误

java - 添加 EditText 时 Fragment 中出现 NullPointerException

android - Ionic 3 上的 OneSignal 推送通知 : All included players are not subscribed

android - 如何将 Android 应用程序添加到已在 iOS 应用程序中使用的现有 Firebase 项目?

javascript - 从 Cloud Functions for Firebase 读取数据?

使用 Android L 最新支持 v4 的 Android Activity 转换

android - 如何检查协程是否使用相同的上下文?

angular - Firebase列表对象到Angular 2中的可观察数组

ios - 为什么我从数据库中提取信息的函数不起作用?

android - 即使应用程序未打开,我应该怎么做才能使 firebase 数据库同步