Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force google account chooser after sign out in android firebase auth

I am using Firebase Google Auth, signing out and logging in again will log in with last signed account. How can I make account chooser every time?

like image 497
flatfisher Avatar asked Dec 07 '16 10:12

flatfisher


People also ask

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.

How do I beat Firebase authentication?

If you haven't yet connected your app to your Firebase project, do so from the Firebase console. Enable Email/Password sign-in: In the Firebase console, open the Auth section. On the Sign in method tab, enable the Email/password sign-in method and click Save.

Which method will you call to logout a user from Firebase?

If you'd like to sign the user out of their current authentication state, call the signOut method: import auth from '@react-native-firebase/auth'; auth() . signOut() .


2 Answers

Firebase Auth Quickstart sample code provides the following few steps for sign out

Declare Globally these two variables

private GoogleSignInClient mGoogleSignInClient;
private GoogleSignInOptions gso;

Add these lines in onCreate method

gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

Now for signOut

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

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

like image 50
akhil Avatar answered Nov 12 '22 07:11

akhil


This part of Firebase Authentication with just a Google Sign In Button in place has given me a lot of nightmares, and if you have encountered the same issue, then I can assure that this answer will save you tons of hours!

Wherever you are implementing the Sign Out functionality, remember to use this line of code and you are good to go.

import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.auth.api.signin.GoogleSignIn


GoogleSignIn.getClient(this, GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).build())
.signOut()

This is a kotlin code.

like image 35
Devansh Nigam Avatar answered Nov 12 '22 06:11

Devansh Nigam