Monday 25 February 2019

Sign in to Google account with old password - how to redirect to blue Google Sign-In page?

I've implement Google Sign-In SDK into my app and all is work fine.

When I click on Sign-In button - I get window with already stored accounts. I select one of them and the whole process with signing in ends with success.

However there is one use case which I cannot pass and I don't know how to solve this:

  • what to do when user gets to Google Sign-In dialog and clicks on account which has changed password?

accounts dialog

I followed with Google instruction "implement Sign-in SDK" and after calling those lines:

Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
GoogleSignInAccount googleSignInAccount = task.getResult(ApiException.class);

I catch exception with status code 12501 SIGN_IN_CANCELLED.

As I said before, it happens because one of the stored accounts has invalid password.

How could I make user to redirect to this blue Google Sign-In page and keep the current flow?

For example, AliExpress somehow can handle this and redirects user to blue page with asking user to sign in again.

enter image description here

My code is not much different than in Google's instruction. This is my code flow. All start from onClick():

In onClick() method:

// Logout before all operations
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
if (account != null) {
    mGoogleSignInClient.signOut();
}

// Call to sign in
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RequestCodes.RC_GOOGLE_SIGN_IN);

In onActivityResult section:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]");

    if (requestCode == RequestCodes.RC_GOOGLE_SIGN_IN) {

        try {

            // Call to take account data
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);

            // Fetch account data
            GoogleSignInAccount googleSignInAccount = task.getResult(ApiException.class);

            Account account = googleSignInAccount.getAccount();

            // Calling to get short lived token
            String shortLivedToken = GoogleAuthUtil.getToken(mContext, account, "oauth2:" + Scopes.PROFILE + " " + Scopes.EMAIL);

            // Further calls here...

        } catch (ApiException e) {

            //https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInStatusCodes

            if (e.getStatusCode() == 12501) {
                Log.e(TAG, "SIGN_IN_CANCELLED");
            } else if (e.getStatusCode() == 12502) {
                Log.e(TAG, "SIGN_IN_CURRENTLY_IN_PROGRESS");
            } else if (e.getStatusCode() == 12500) {
                Log.e(TAG, "SIGN_IN_FAILED");
            } else {
                e.printStackTrace();
            }

        } catch (GoogleAuthException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}



from Sign in to Google account with old password - how to redirect to blue Google Sign-In page?

No comments:

Post a Comment