google signin uid&在firebase升级到9.2.0后,firebase uid不匹配

时间:2016-07-15 17:45:01

标签: android firebase firebase-authentication google-signin

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

升级前 -

Google Signin uid = 101672719428298324455

Firebase uid = google:101672719428298324455

升级后 -

Google Signin uid = 101672719428298324455

Firebase uid = fcojpImyQWTHp02YzWYsRezShKP2

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

但是,那么,我们如何使用google signin uid和升级后的firebase为auth编写安全规则?规则的具体用例是教师可以阅读学生的成绩单。谷歌教室为班级名单提供的教师和学生uids与google登录uids匹配,而不是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();

课程的学生使用谷歌课堂

加载
"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);

1 个答案:

答案 0 :(得分:2)

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

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

  

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

currentUser

您会注意到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个对象的列表,因为单个用户帐户可以有多个链接的提供商。