42

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().

Pavlo28
  • 1,454
  • 2
  • 16
  • 30
  • Code or it didn't happen. Let's see your logic for showing the popup. – Chad Bingham Aug 01 '16 at 20:20
  • 1
    A quick guess: calling `signOut()` signs the user our of Firebase Authentication. It does not sign the user out of the underlying social provider (e.g. Facebook, Google). So next time your start the sign-in flow for that provider, it will be picked up immediately by your code (or the library that you used). But it'll be easier indeed to say what's going if you show the [minimal code that reproduces the problem](http://stackoverflow.com/help/mcve). – Frank van Puffelen Aug 01 '16 at 20:31
  • You might get some clues by comparing your code with the [Firebase Auth Quickstart project](https://github.com/firebase/quickstart-android/tree/master/auth). When I run it using Google Signin, the account picker dialog is presented every time. Don't know if that is true for Twitter, Facebook, etc. – Bob Snyder Aug 01 '16 at 20:46
  • @FrankvanPuffelen added the code – Pavlo28 Aug 01 '16 at 20:46
  • @qbix google authentication – Pavlo28 Aug 01 '16 at 20:48
  • If you got here experiencing this in your JS/web app, the answer is slightly different because you really don't want to be logged out of Google across all tabs/windows in your browser (vs. a single Android app): https://stackoverflow.com/a/59744590/305689 – wescpy Jan 15 '20 at 03:11

11 Answers11

66

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);
                }
            });
}
Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
17

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

ZooS
  • 658
  • 8
  • 18
  • Clean elegant solution .. self-contained, doesn't require involving GoogleApiClient reference. Nice, thanks! – Gene Bo Oct 29 '18 at 01:25
  • With latest Gradle and Android Studio updates in projects using Java 8 listener can be shortened to `.addOnCompleteListener(task -> { /* do something here */ });` – Variag May 29 '19 at 15:12
  • YESSSSS THANK YOU. The other solutions were not working but this one did. – Peter Dec 14 '22 at 20:11
15

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();
Sharp
  • 1,335
  • 1
  • 12
  • 27
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!

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Alex Barbu
  • 41
  • 3
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);
                }
            });
}
2

None of the above did fix the issue for me,

  1. accepted answer requires access to mGoogleApiClient defined in login view (normally log out button is in settings view)

  2. another answer was suggesting to logout in the login view (after successful login and just before launching to the main view). This was addressing the mGoogleApiClient accessibility issue, but the issue with this approach is every time user opens the app it ends up in login view and requires to log in every time which is not ideal.

So here is what I ended up doing and it is kind of all-round fix that support all the third-party log out options (in my case Facebook and Google)

    logOutButton.setOnSingleClickListener {
            FirebaseAuth.getInstance().currentUser?.getIdToken(false)?.addOnSuccessListener {
                    result ->
                when (result.signInProvider){
                    "facebook.com" -> {
                        LoginManager.getInstance().logOut()
                        signOutFromApp()
                    }
                    "google.com" -> {
                        GoogleSignIn.getClient(
                            this,
                            GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).build()
                        ).signOut()
                        signOutFromApp()
                    }
                    else -> {
                        signOutFromApp()
                    }
                }
            }
        }

   private fun signOutFromApp() {
        FirebaseAuth.getInstance().signOut()
        LauncherActivity.start(this) //starts login view
        finish() //finish settigs view 
    }
bastami82
  • 5,955
  • 7
  • 33
  • 44
1
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

Sumer Singh
  • 468
  • 4
  • 9
1

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

Ankit Saini
  • 374
  • 5
  • 15
1

you can set a custom parameter for the GoogleAuthProvider to force user to re authenticate via google.

var provider = new Firebase.auth.GoogleAuthProvider();
      provider.setCustomParameters({
        prompt: 'select_account'
      });
Shib
  • 76
  • 3
0

Use same instances of Firebase Auth and GoogleSignInClient,
for example, If we declared and instantiated a Firebase Auth called mAuth in LoginActivity,
Then if we declare a new mAuth in other activity and try to call mAuth.login, it will not work properly.
Make Firebase Auth and GoogleSignInClient variables public and static and use the same from the other activities

Sumanth Hegde
  • 71
  • 1
  • 6
0

For flutter use,

await GoogleSignIn().signOut();
await _auth.signOut();
Soorya
  • 641
  • 9
  • 16