Saurabh Sharma
Saurabh Sharma

Reputation: 15

What is the Difference between acct.getId() vs mFirebaseUser.getUid()?

I am trying to save user data into firebase realtime database and I used google sign in authentication. So while creating the users node tree, I want to store data like "Users">"uid">userdata. How to get Uid of authenticated user? by acct.getId() [acct is GoogleSignInAccount object] OR user.getUid() [user is FirebaseUser object] ? Code Starts from here:

 private void firebaseAuthWithGoogle (final GoogleSignInAccount acct) {
    Log.d("TAG", "firebaseAuthWithGoogle:" + acct.getId());


    final DatabaseReference rootRef;
    rootRef = FirebaseDatabase.getInstance().getReference();

    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Log.d("TAG", "signInWithCredential:success");
                        FirebaseUser user = mAuth.getCurrentUser();

                        if (acct != null) {
                            personName = acct.getDisplayName();
                            uid=user.getUid();
                            uid1=acct.getId();
                            HashMap<String,Object> userdatamap = new HashMap<>();
                            userdatamap.put("firstName",personName);
                            userdatamap.put("email",acct.getEmail());
                            rootRef.child("Users").child(/* uid */).updateChildren(userdatamap)
                                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task) {
                                            if(task.isSuccessful()){
                                                Toast.makeText(login.this,"DONE",Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    });

                            pref = getApplicationContext().getSharedPreferences("userinfo",0);
                            editor = pref.edit();
                            editor.putString("namekey",personName);
                            editor.commit();
                            Toast.makeText(login.this,"Welcome "+personName,Toast.LENGTH_SHORT).show();

                        }
                        else {
                            Toast.makeText(login.this,"Profile Name Not Found",Toast.LENGTH_SHORT).show();
                        }
                        //updateUI(user);

                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w("TAG", "signInWithCredential:failure", task.getException());
                        Toast.makeText(login.this,"Auth Failed",Toast.LENGTH_SHORT).show();
                        //updateUI(null);
                    }

                    // ...
                }
            });
}

Upvotes: 0

Views: 379

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

GoogleSignInAccount's getId():

Returns the unique ID for the Google account if you built your configuration starting from `new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) or with requestId() configured; null otherwise.

This is the preferred unique key to use for a user record.

While FirebaseUser's 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.

This identifier is opaque and does not correspond necessarily to the user's email address or any other field.

When using Firebase services it's best to use the uid that comes from the Firebase authentication. From the official documentation:

After a user signs in for the first time, a new user account is created and linked to the credentials—that is, the user name and password, phone number, or auth provider information—the user signed in with. This new account is stored as part of your Firebase project, and can be used to identify a user across every app in your project, regardless of how the user signs in.

Upvotes: 1

Related Questions