Google Sigin problem when re-logging into Firebase

Asked

Viewed 477 times

1

I have a little problem logging into Firebase using the Google Sigin. The point is that I am able to log in with this method, but when there is a problem after selecting some account (figure below) I can’t choose another account to try to log in again, it insists on the same account, but not authenticating makes it impossible to proceed.

The idea I want is that, when generating the error, it allows me to choose another account, or that it shows me this popup again.

I don’t want to put another login method in for now.

Follow 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();

    }

    }

1 answer

0


For some reason, it is caching the solution. What you can do is, when it is wrong, force the signout this way (put below Toast):

Auth.GoogleSignInApi.signOut(mGoogleApiClient);

I believe that when you press to login again, it will show the selection. Remembering that if the user leaves the application (if yours has an option to quit), it should also be called.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.