Google Firebase sign out and forget user in Androi

2019-01-06 18:44发布

问题:

When I call mFirebaseAuth.signOut() or mFirebaseUser.delete() my FirebaseAuth.AuthStateListener() works correct and returns null as FirebaseUser instance in onAuthStateChanged, I refresh UI and show "Sign in with Google" button.

But when I want to log in again, I don't see the pop-up with users (I have 2 users on my device, attached the image). The app shows this pop-up only in first sign in, after that it uses the same user. If I clear app's data on the settings screen I will be able to see this pop-up again.

My question is how to show this pop-up after every sign out.

I run this code when press Sign In button:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
            startActivityForResult(signInIntent, FirebaseActivity.REQUEST_SIGN_IN);

In onActivityResult(), I get an instance of GoogleSignInResult so everything I need happens after I call startActivityForResult().

回答1:

In the Firebase Auth Quickstart sample code, the sign-out for Google provider includes these steps. Are you calling GoogleSignInClient.signOut() when you sign-out?

private void signOut() {
    // Firebase sign out
    mAuth.signOut();

    // Google sign out
    mGoogleSignInClient.signOut().addOnCompleteListener(this,
            new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    updateUI(null);
                }
            });
}


回答2:

Another option is to use the FirebaseUI library. It simplifies sign in and sign out operations in a sense that it will do all the heavy lifting for you.

Kotlin

AuthUI.getInstance().signOut(this).addOnCompleteListener { 
    // do something here 
}

Java

AuthUI.getInstance()
      .signOut(ActivityMainOld.this)
      .addOnCompleteListener(new OnCompleteListener<Void>(){

          @Override
          public void onComplete(@NonNull Task<Void> task) {

              // do something here

          }
      });

Hope this helps



回答3:

For anyone else who wants this result (as in getting the google account options back) on a different activity.

public static void signOut() {
    // Firebase sign out
    mAuth.signOut();

    // Google sign out
    Auth.GoogleSignInApi.signOut(mGoogleApiClient);
}

Add this on the sign in page, and before you pass to the next activity, just call SignOut().

// everything ok...             
signOut();
startActivity(new Intent(SignIn.this,NextOne.class));

and then, in your other class you can call

FirebaseAuth.getInstance().signOut();
startActivity(new Intent(NextClass.this, SignIn.class));

It's easy, and it will work. Cheers!



回答4:

You can also define something like this:

private void signOut() {
    mAuth.signOut();
    mGoogleSignInClient.signOut().addOnCompleteListener(this,
            new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    Intent intent = new Intent(YourActivity.this, NextActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                }
            });
}


回答5:

private void sendToLogin() { //funtion
    GoogleSignInClient mGoogleSignInClient ;
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(getString(R.string.default_web_client_id))
        .requestEmail()
        .build();
    mGoogleSignInClient = GoogleSignIn.getClient(getBaseContext(), gso);
    mGoogleSignInClient.signOut().addOnCompleteListener(/*CURRENT CLASS */.this,
        new OnCompleteListener<Void>() {  //signout Google
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                FirebaseAuth.getInstance().signOut(); //signout firebase
                Intent setupIntent = new Intent(getBaseContext(), /*To ur activity calss*/);
                Toast.makeText(getBaseContext(), "Logged Out", Toast.LENGTH_LONG).show(); //if u want to show some text
                setupIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(setupIntent);
                finish();
            }
        });
}

this code is written to work as copy past just read COMMENTS in code to customize it to ur needs, i prefer to send user to login



回答6:

I did mGoogleSignInClient.signOut() this is working as expected.