4
votes

BACKGROUND

I am using Google Plus OAuth in my app along with Firebase. When the user taps the sign in button, and account selection dialog appears and the user selects the particular account of choice and signs in.

PROBLEM

Suppose the user has two accounts in that device, A and B. Initially, he selects account A to sign in and then uses the app and then decides to sign out. Now he clicks the sign in button again, but the account selection dialog doesn't appear and it automatically signs in using the previously selected account A.

WHAT I WANT

I want the user to be presented with the account selection dialog every time and give them a chance to select a different account if they want to.

CODE

Here is a small code snippet of how I am using the Google Api Client for sign in.

  String mClientId = parcel.getProviderExtra().getString(CLIENT_ID_KEY);
        GoogleSignInOptions googleSignInOptions;

        googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(mClientId)
                .requestEmail()
                .build();

        mGoogleApiClient = new GoogleApiClient.Builder(activity)
                .addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
                .build();

        mGoogleApiClient.connect();

and sign out,

Auth.GoogleSignInApi.signOut(mGoogleApiClient);

Any help will be highly appreciated.

3
you can logout the user before mGoogleApiClient.connect(); with app. conditions - Arpan Sharma
The sign out API doesn't help. The dialog doesn't show when the user logs out. The only way is to clear data, that clears some preference that it stores and then the dialog starts appearing again. - Aritra Roy

3 Answers

2
votes

Got this resolved by clearing the default account every time before sign in.

@Override
    public void startLogin(Activity activity, String mEmail) {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        /**
         * Clearing default account every time so that the account picker dialog can be enforced
         */
        if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
            mGoogleApiClient.clearDefaultAccountAndReconnect();
        }
        activity.startActivityForResult(signInIntent, RC_SIGN_IN);
    }

This will enforce the dialog to appear every time.

1
votes
    private void revokeAccess() {
        googleSignInClient.revokeAccess()
                .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        // ...
                        Log.d(LOGTAG, " /// revokeAccess() /// ");
                    }
                });
    }
0
votes

Just clear the default account everytime the user sign's in, by using the method below.

    private void signIn() {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);

    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {

        mGoogleApiClient.clearDefaultAccountAndReconnect();
    }
    startActivityForResult(signInIntent, RC_SIGN_IN);
}