Google Firebase sign out and forget user in Android app

34,539

Solution 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);
                }
            });
}

Solution 2

I was confused since all of the solutions required having a reference to the GoogleSignInClient, but it is actually not required for you to hold a reference to it, you can simply create a new instance and call signOut() on it.

GoogleSignIn.getClient(
    getContext(), 
    new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).build()
).signOut();

Solution 3

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

Solution 4

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!

Solution 5

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);
                }
            });
}
Share:
34,539
GrafOrlov
Author by

GrafOrlov

Updated on July 26, 2021

Comments

  • GrafOrlov
    GrafOrlov almost 3 years

    When I call mFirebaseAuth.signOut() or mFirebaseUser.delete() my FirebaseAuth.AuthStateListener() works correctly 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 dialog with users (I have 2 users on my device, attached the image). The app shows this dialog 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 dialog again.

    My question is how to show this dialog after every sign out.

    enter image description here

    I run this code when press Sign In button:

    // in onCreate()
    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();
    
    // in OnClickListener
    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().

  • GrafOrlov
    GrafOrlov almost 8 years
    No, i didn't call Auth.GoogleSignInApi.signOut() . Thanks a lot, it works now.
  • Frank van Puffelen
    Frank van Puffelen almost 8 years
    Good find @qbix! I forgot that part of the quickstart code.
  • Bob Snyder
    Bob Snyder almost 8 years
    Credit really goes to you @FrankvanPuffelen. Your earlier comment put me on the trail.
  • Ali Bdeir
    Ali Bdeir almost 8 years
    How do I get mGoogleApiClient? And what does updateUI do? How do I do this with Facebook? I have logout in a separate class. Thanks
  • Bob Snyder
    Bob Snyder almost 8 years
    @AbAppletic: Look in the Quickstart sample code linked in the answer.
  • Ali Bdeir
    Ali Bdeir almost 8 years
    @qbix what I didn't understand is that why is there a signOut() method in the SIGN IN activity?
  • Talha
    Talha over 7 years
    Hi @qbix, how we can do this for firebase web?
  • coderpc
    coderpc about 7 years
    How can we do that with Firebase's AuthUI Google Provider. Can you please help! Have a look at this problem
  • Gene Bo
    Gene Bo over 5 years
    Clean elegant solution .. self-contained, doesn't require involving GoogleApiClient reference. Nice, thanks!
  • Bob Snyder
    Bob Snyder over 5 years
    Thanks, @ArnaudSmartFun. I fixed the link to sample code.
  • Variag
    Variag about 5 years
    With latest Gradle and Android Studio updates in projects using Java 8 listener can be shortened to .addOnCompleteListener(task -> { /* do something here */ });