firebase 升级到 9.2.0 后,google 登录 uid 和 firebase uid 不匹配

Ana*_*ond 5 android firebase firebase-authentication google-signin

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

升级前——

谷歌登录 uid = 101672719428298324455

Firebase uid = 谷歌:101672719428298324455

升级后——

谷歌登录 uid = 101672719428298324455

Firebase uid = fcojpImyQWTHp02YzWYsRezShKP2

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

但是,那么,我们如何使用谷歌登录 uid 和升级后的 Firebase 为身份验证编写安全规则?规则的具体用例是教师可以阅读学生的成绩单。谷歌课堂为班级名册提供的教师和学生 uid 与谷歌登录 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();
Run Code Online (Sandbox Code Playgroud)

使用 google-classroom 加载班级的学生

"google:101379167706178411999": {
  "profile" : {
    "course" : {
      "students" : {
        "google:102942138935686001927" : {
          "profile" : {
            "name" : "student1 U."
          }
        },
        "google:111992383609839990527" : {
          "profile" : {
            "name" : "student2 U."
          }
        }
      }
    },
    "email" : "...",
    "name" : "teacher User",
  }
}
Run Code Online (Sandbox Code Playgroud)

然后,老师使用谷歌登录 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);
Run Code Online (Sandbox Code Playgroud)

Fra*_*len 2

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

如果您想知道使用 Google 帐户登录 Firebase 身份验证的用户的 Google ID,您可以从字段中查找currentUser。来自有关访问用户特定于提供商的个人资料信息的文档:

要获取从链接到用户的登录提供程序检索到的配置文件信息,请使用 getProviderData 方法。例如:

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();
    };
}
Run Code Online (Sandbox Code Playgroud)

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