简体   繁体   中英

Firebase Auth cannot sign out using Android

I am totally new to Android and firebase and I am having some issues when I try to sign out from Firebase.

A piece of my code is following at the bottom..

In the onAuthStateChanged listener I try to create a Firebase user and get the current user in order to check if it is NOT null (which according to the Firebase docs means the user is signed in) or null which means the user is not signed in..

Then comes the problem..

Out of testing / curiosity purposes, if the user is signed in, I try to update the user profile and set a custom display name.. to see what happens.

It appears that after updating the profile and more specifically the line "user.updateProfile(profileUpdates);" breaks the signout() function.

If I comment out the line "user.updateProfile(profileUpdates);" the signout() function seems to be working fine!

Can you point out what am I doing wrong?

Are there duplicate instances of the firebase user somewhere?

Thanks in advance!

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private FirebaseAuth FireAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseDatabase mFireDatabase;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    FireAuth = FirebaseAuth.getInstance();

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

            FirebaseUser user = firebaseAuth.getCurrentUser();

            if (user != null) {
                // already signed in
                UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                        .setDisplayName("SuperMan").build();
                user.updateProfile(profileUpdates); // This breaks it!
                signoutButton.setText(user.getDisplayName().toString());

            } else {
                // not signed in

            }

        }
    };

}

public void signin(String email, String password){

   FireAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            Log.d("myTEST","signInWithEmailAndPassword:complete:"+task.isSuccessful());
        }
    });
}
private void signout(){
    //FireAuth.getInstance().signOut();
    FireAuth.signOut();

}

public void getDisplayName(){

    if (FireAuth.getCurrentUser() !=null){
        UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                .setDisplayName("SuperMan").build();

        FireAuth.getCurrentUser().updateProfile(profileUpdates);
        signoutButton.setText(FireAuth.getCurrentUser().getUid().toString());
        if(FireAuth.getCurrentUser().isAnonymous()){
            signoutButton.setText("Anonymous");
        }

    }else{
        signoutButton.setText("empty..");
    }
}


@Override
public void onStart() {
    super.onStart();
    FireAuth.addAuthStateListener(mAuthListener);
}

@Override
public void onStop(){
    super.onStop();
    FireAuth.removeAuthStateListener(mAuthListener);
}


@Override
public void onClick(View v){
    switch (v.getId()){
        case R.id.signinButton:
            signin(loginEditText.getText().toString(),passwordEditText.getText().toString());
        break;
        case R.id.signoutButton:
            signout();
        break;
        case R.id.button3:
            getDisplayName();
            break;

    }

}}

A couple of observations that might explain the behavior you are seeing.

First, FirebaseUser.updateProfile() may require a roundtrip to the Firebase server and completes asynchronously. You can add a completion listener with a Log message to get an understanding of this:

UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
        .setDisplayName("SuperMan").build();
user.updateProfile(profileUpdates)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        Log.d(TAG, "onComplete: " + user.getDisplayName());
    }
});
Log.d(TAG, "Profile change requested");

Second, changing the user profile appears to cause the AuthStateListener to fire, which will cause your code to loop. Adding some Log statements would confirm this is happening. I suspect the looping is interfering with the sign-out processing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM