android - firebase 升级到 9.2.0 后,google signin uid 和 firebase uid 不匹配

标签 android firebase firebase-authentication google-signin

我将 firebase 数据库升级到版本 9.2.0。 firebase uid 曾经是 google:(google signin id),但现在不匹配。

升级前-

Google 登录 uid = 101672719428298324455

Firebase uid = google:101672719428298324455

升级后-

Google 登录 uid = 101672719428298324455

Firebase uid = fcojpImyQWTHp02YzWYsRezShKP2

google uid 由其他服务返回,例如 classroom,因此,我们需要使用它作为 uid 来告诉它是哪个用户。我们将更新用户字段以使用 google 登录 uid 而不是 firebase uid。

但是,那么,我们如何使用带有升级的 firebase 的 google 登录 uid 为 auth 编写安全规则?规则的具体用例是教师可以阅读学生的成绩单。 google classroom 为类(class)花名册提供的教师和学生 uid 匹配 google 登录 uid,而不是 firebase uid。

升级后用于登录的代码下方 -

FirebaseAuth auth = FirebaseAuth.getInstance();
        AuthCredential credential = GoogleAuthProvider.getCredential(token, secret);
        auth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                // Authenticated successfully with payload authData
                AuthResult result = task.getResult();
                FirebaseUser user = result.getUser();

类(class)的学生使用 google-classroom 加载

"google:101379167706178411999": {
  "profile" : {
    "course" : {
      "students" : {
        "google:102942138935686001927" : {
          "profile" : {
            "name" : "student1 U."
          }
        },
        "google:111992383609839990527" : {
          "profile" : {
            "name" : "student2 U."
          }
        }
      }
    },
    "email" : "...",
    "name" : "teacher User",
  }
}

然后,老师使用谷歌登录 ID 查询学生 -

 queryRef = mFirebase.child("users").child(uid).child("profile").child("course").child("students").orderByKey();
        listListener = queryRef.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot snapshot, String previousChild) {
                Log.d("DashboardDataHandler", "Attaching listener to: " + snapshot.getKey());
                final String key = snapshot.getKey();
                addOrUpdateUserProfile(snapshot, key);

最佳答案

对于现有用户,当您将现有项目导入新的 Firebase 控制台 (https://console.firebase.google.com) 时,他们的 uid 不会改变。但导入项目后创建的用户将获得新的 uid 格式。这是有意为之:您不应依赖 Firebase 提供的 UID 中的任何固有结构。

如果您想知道使用 Google 帐户登录 Firebase 身份验证的用户的 Google ID,您可以从 currentUser 字段中查找。来自 accessing a user's provider-specific profile information 上的文档:

To get the profile information retrieved from the sign-in providers linked to a user, use the getProviderData method. For example:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    for (UserInfo profile : user.getProviderData()) {
        // Id of the provider (ex: google.com)
        String providerId = profile.getProviderId();

        // UID specific to the provider
        String uid = profile.getUid();

        // Name, email address, and profile photo Url
        String name = profile.getDisplayName();
        String email = profile.getEmail();
        Uri photoUrl = profile.getPhotoUrl();
    };
}

您会注意到,如今 getProviderData 会返回一个 UserInfo 对象列表,因为单个用户帐户可以有多个链接的提供商。

关于android - firebase 升级到 9.2.0 后,google signin uid 和 firebase uid 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38402278/

相关文章:

android - Android 上的 flash.sensors.Accelerometer 在 Web 浏览器中

javascript - (Vue 和 Firebase)我在一个数组中保存多个图像时遇到问题

angular - 使用 AngularFireObject 和 switchMap

c# - 如何在Unity中调用Firebase电子邮件登录任务中的函数?

android - 验证多个 EditText

java - 无法使用具有不同服务器端口的 HttpClient 下载

android - NDK 构建失败

android - Firebase Firestore - 移动 SDK 和 Web SDK 之间的差异

android - 如何在我的 Kotlin firebase 应用程序中保留身份验证状态?

firebase - 配置根项目 'firebase_auth' 时出现问题