Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Firebase sign out and forget user in Android app

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

like image 943
GrafOrlov Avatar asked Aug 01 '16 19:08

GrafOrlov


People also ask

How do I remove a user from Firebase authentication?

You can also delete users from the Authentication section of the Firebase console, on the Users page. Important: To delete a user, the user must have signed in recently. See Re-authenticate a user.

How do I sign out of Google Firebase?

If signing in is successful, the developer console will log in our user. We can also click on the Google Signout button to logout from the app. The console will confirm that the logout was successful.

Why Firebase user still signed in after I deleted it from Firebase dashboard?

The underlying ID token that authenticates refreshes every hour. So, after an account is deleted, the client will be authenticated for up to one hour. Only until it tries to refresh the ID token will the client become unauthenticated.

How do I detect if a user is already logged in Firebase?

To detect if a user is already logged in Firebase with JavaScript, we can call the onAuthStateChanged method. firebase. auth(). onAuthStateChanged((user) => { if (user) { // ... } else { // ... } });


3 Answers

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);
                }
            });
}
like image 187
Bob Snyder Avatar answered Oct 11 '22 08:10

Bob Snyder


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

like image 15
ZooS Avatar answered Oct 11 '22 08:10

ZooS


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();
like image 14
Sharp Avatar answered Oct 11 '22 09:10

Sharp