I use OAuth to let users sign in to the android app via Google account. When the user taps the Google login button for the first time, it produces a dialog to choose the acc
Those who needs it in kotlin, this worked for me, I do clear current login cache before a new login request is made
/**
* signInGoogle
*/
private fun signInGoogle() = launch{
//init google sigin
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(GOOGLE_WEBCLIENT_ID)
.requestEmail()
.build()
val gApiClient = GoogleApiClient.Builder(activity)
.addApi(Auth.GOOGLE_SIGN_IN_API,gso)
.build()
val c = gApiClient.blockingConnect()
if(c.isSuccess && gApiClient.isConnected){
gApiClient.clearDefaultAccountAndReconnect().await()
}
val mGoogleSignInClient = GoogleSignIn.getClient(activity, gso)
val signInIntent = mGoogleSignInClient.signInIntent
//start activit
startActivityForResult(signInIntent, GOOGLE_SIGNIN)
}//end request google login
You can view the full source here : https://github.com/transcodium/TNSMoney-Android/blob/master/app/src/main/java/com/transcodium/tnsmoney/SocialLoginActivity.kt
Simplely workout for me
1>Activity implements GoogleApiClient.OnConnectionFailedListener {
2>
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
3>In OnButtonClicked
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, 2);
4>
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
GoogleSignInAccount acct = result.getSignInAccount();
if(acct!=null) {
//Take all data You Want
String identifier = acct.getId()+"";
String displayName = acct.getDisplayName()+"";
//After You got your data add this to clear the priviously selected mail
mGoogleApiClient.clearDefaultAccountAndReconnect();
}
}else Log.e("handleSignInResult","Failed ; "+result.getStatus());
}
}
You have to just clear pre-signed in accounts. To do so, add the following as first line under signIn() function: mGoogleSignInClient.signOut();
private void signIn() {
// clearing previous signin caches
mGoogleSignInClient.signOut();
//getting the google signin intent
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
//starting the activity for result
startActivityForResult(signInIntent, RC_SIGN_IN);
}
Just add mGoogleSignInClient.signOut(); After succesfully handling sign in.
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
mAuth.signInWithCredential(credential).addOnCompleteListener(this, task -> {
if(task.isSuccessful()) {
mGoogleSignInClient.signOut();
updateUI();
} else {
Toast.makeText(this, "Something went wrong.", Toast.LENGTH_SHORT).show();
}
});
} catch (ApiException e) {
Log.w("SignIn Failed", "signInResult:failed code=" + e.getStatusCode());
updateUI(null);
}
}
I found this question while searching for a solution using the javascript client, but it might be similar with Android.
signIn({ prompt: 'select_account' })
select_account
The authorization server prompts the user to select a Google account. This allows a user who has multiple accounts to select amongst the multiple accounts that they may have current sessions for.
See https://developers.google.com/identity/sign-in/web/reference#googleauthsigninoptions.
I am using the Firebase Auth UI, and For me the fix was;
AuthUI.getInstance().setIsSmartLockEnabled(false)...
When logging in, and then;
AuthUI.signOut(context)
When Signing out