mjsxbo
mjsxbo

Reputation: 2266

Does FirebaseUser.getUid() give the same id if the user logs out and logs in again?

I'm using google sign-in along with firebase in my app. I'm using the unique id generated by:

 FirebaseAuth.getInstance().getCurrentUser().getUid()

to save data for each user in my firebase database. If the user logs out and then logs in again using google-sign-in, will this command generate the same key the second time as well?

I don't think it's required, but following is the part of my code responsible once the user logs in using google sign-in.

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            showProgressDialog("Signing In ....");
            if (result.isSuccess()) {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = result.getSignInAccount();
                firebaseAuthWithGoogle(account);
            } else {
                hideProgressDialog();
                showSnack("Sign In failed");
                showToast(result.getStatus().getStatusMessage());
            }
        }
    }


    private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {

        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()) {
                            showSnack("Sign In failed.");
                            showToast(task.getException().getMessage());
                            hideProgressDialog();
                        }
                    }
                });
    }

Upvotes: 0

Views: 596

Answers (2)

CanDroid
CanDroid

Reputation: 643

Yes it is unique. FirebaseUser.getUid() returns always same and unique ID for each user. Please note that there are many other providers on Firebase and each provider can return different IDs but FirebaseUser.getUid() returns always same. Please read this link: https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseUser#getUid()

Upvotes: 3

GGWP
GGWP

Reputation: 1121

This can be very easy to tell.

Go to your Authentication tab in your Firebase console uid

And watch it yourself if the UID is changing everytime the user logs in and out

As far as on my observation the UID still remains the same even when the user logs in and out.

Upvotes: 0

Related Questions