gpt4 book ai didi

android - Firebase 保存数据用户相关

转载 作者:搜寻专家 更新时间:2023-11-01 09:31:40 25 4
gpt4 key购买 nike

我目前正在编写一个 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 选择所有 ObjectRatings 来计算平均评分?

最佳答案

要获取用户的唯一标识符,您可以使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com