I'm having a small problem logging into Firebase using Google SigIn . The problem is that I am able to log in with this method, but when there is a problem after selecting an account (figure below) I can not choose another account to try to log in again, it insists on the same account, but not authenticate it is impracticable to proceed.
The idea I want is that by generating the error, it allows me to choose another account, or to have it popup again.
I do not want to put in another login method for now.
Here is the code I'm using:
package com.nome_do_pacote.login;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.nome_do_pacote.R;
import com.nome_do_pacote.data_manager.LibraryIO;
import com.nome_do_pacote.listener_firebase.CustomValueEventListener;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;
import com.google.firebase.database.DatabaseReference;
public class Login extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient googleApiClient;
private SignInButton signInButton;
public static final int SIGN_IN_CODE = 777;
private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener firebaseAuthListener;
private ProgressBar progressBar;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tela_login_in);
//Adicionando uma Toolbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
googleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
signInButton = (SignInButton) findViewById(R.id.signInButton);
signInButton.setSize(SignInButton.SIZE_WIDE);
signInButton.setColorScheme(SignInButton.COLOR_DARK);
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
startActivityForResult(intent, SIGN_IN_CODE);
}
});
firebaseAuth = FirebaseAuth.getInstance();
firebaseAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
downloadFirebase();
goMainScreen();
}
}
};
progressBar = (ProgressBar) findViewById(R.id.progressBar);
}
@Override
protected void onStart() {
super.onStart();
firebaseAuth.addAuthStateListener(firebaseAuthListener);
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SIGN_IN_CODE) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
if (result.isSuccess()) {
firebaseAuthWithGoogle(result.getSignInAccount());
} else {
Toast.makeText(getApplicationContext(), R.string.not_log_in, Toast.LENGTH_LONG).show();
//É esse toast que aparece quando ocorre o problema.
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount signInAccount) {
progressBar.setVisibility(View.VISIBLE);
signInButton.setVisibility(View.GONE);
final AuthCredential credential = GoogleAuthProvider.getCredential(signInAccount.getIdToken(), null);
firebaseAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressBar.setVisibility(View.GONE);
signInButton.setVisibility(View.VISIBLE);
if (!task.isSuccessful()) {
Toast.makeText(getApplicationContext(), R.string.not_firebase_auth, Toast.LENGTH_LONG).show();
}
}
});
}
private void goMainScreen() {
Intent intent = new Intent(this, MainLogin.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
@Override
protected void onStop() {
super.onStop();
if (firebaseAuthListener != null) {
firebaseAuth.removeAuthStateListener(firebaseAuthListener);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_contato, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_contato:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setData(Uri.parse("email"));
String[] email = {getResources().getString(R.string.mEmail)};
intent.putExtra(Intent.EXTRA_EMAIL, email);
intent.setType("menssage/rfc822");
try {
startActivity(Intent.createChooser(intent, getString(R.string.selecion_email)));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(), R.string.nao_existe_email_instalado, Toast.LENGTH_SHORT).show();
}
break;
}
return super.onOptionsItemSelected(item);
}
DatabaseReference mDataBase = LibraryIO.getFirebaseRef().child("horarios");
private CustomValueEventListener customValueEventListener;
private void downloadFirebase(){
Toast.makeText(getApplicationContext(), R.string.baixandolistasdehorarios, Toast.LENGTH_LONG).show();
customValueEventListener = new CustomValueEventListener(Login.this);
mDataBase.addValueEventListener(customValueEventListener);
Toast.makeText(getApplicationContext(), R.string.tudo_pronto, Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
if (customValueEventListener != null) {
mDataBase.removeEventListener(customValueEventListener);
}
super.onDestroy();
}
}