Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google signin uid & firebase uid don't match after firebase upgrade to 9.2.0

I upgraded firebase database to version 9.2.0. The firebase uid used to be google:(google signin id), but, it doesn't match now.

Before upgrade -

Google Signin uid = 101672719428298324455

Firebase uid = google:101672719428298324455

After upgrade -

Google Signin uid = 101672719428298324455

Firebase uid = fcojpImyQWTHp02YzWYsRezShKP2

The google uid is returned by other services such as classroom, so, we need to use that as the uid to tell which user it is. We will update the users field to use the google sign-in uid instead of the firebase one.

But, then, how do we write security rules for auth using the google signin uid with the upgraded firebase? Specific use case for the rules is that a teacher can read a student's report card. Teacher and student uids provided by google classroom for the class roster match the google sign-in uids, not firebase uid.

Below the the code being used to login after the upgrade -

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();

The students for the class are loaded using google-classroom

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

Then, teacher uses the google signin ids to query the student -

 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);
like image 646
Ana Redmond Avatar asked Oct 30 '22 00:10

Ana Redmond


1 Answers

For existing users, their uid will not change when you import an existing project into the new Firebase console (https://console.firebase.google.com). But users created after importing the project will get the new uid format. This is intentional: you should not rely on any inherent structure in the UID Firebase provides.

If you want to know the Google ID for a user that is signed in to Firebase Authentication with a Google account, you can look that up from the currentUser field. From the documentation on 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();
    };
}

You'll note that getProviderData returns a list of UserInfo objects nowadays, since a single user account can have multiple linked providers.

like image 86
Frank van Puffelen Avatar answered Nov 08 '22 06:11

Frank van Puffelen