Hi i am having a problem with my the google connexion. Every time that the code is executed it goes in to the catch. I dont know why. I followed the steps from the Google tutorial and saw manyq questions related to that on stack but still not help me to solve this issue. Whe the code is runned it goes in to the catch and it shows the toast that i made. " error while .. ".
Here is what it looks like. I am posting the whole code for the authentification so everyone can analyze it and give me a solution if possible .
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient= GoogleSignIn.getClient(this,gso);
signInButton=(SignInButton)findViewById(R.id.googleButton);
signInButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
});
//pour google
mAuthStateListener=new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser mFireBaseUser=mFirebaseAuth.getCurrentUser();
if(mFireBaseUser != null){
Toast.makeText(LoginActivity.this,"you are logged in",Toast.LENGTH_SHORT).show();
Intent i= new Intent(LoginActivity.this, PrayersEvents.class);
startActivity(i);
}
else{
Toast.makeText(LoginActivity.this,"please login",Toast.LENGTH_SHORT).show();
}
}
};
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email=emailId.getText().toString();
String pwd=password.getText().toString();
if (email.isEmpty()) {
emailId.setError("Please enter email");
emailId.requestFocus();
}
else if(pwd.isEmpty()){
password.setError("Please enter your password");
password.requestFocus();
}
else if(email.isEmpty() || pwd.isEmpty()){
Toast.makeText(LoginActivity.this,"Fields are empty!",Toast.LENGTH_SHORT).show();
}
else if(!(email.isEmpty() && pwd.isEmpty())){
mFirebaseAuth.signInWithEmailAndPassword(email,pwd).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(!task.isSuccessful()){
Toast.makeText(LoginActivity.this,"Login error!",Toast.LENGTH_SHORT).show();
}
else{
Intent intoHome=new Intent(LoginActivity.this, PrayersEvents.class);
startActivity(intoHome);
}
}
});
}
else{
Toast.makeText(LoginActivity.this,"Error ocurred!",Toast.LENGTH_SHORT).show();
}
}
});
tvSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intoSignUp=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intoSignUp);
}
});
}
@Override
protected void onStart() {
super.onStart();
GoogleSignInAccount account=GoogleSignIn.getLastSignedInAccount(this);
if(account !=null){
startActivity(new Intent(LoginActivity.this, CreateEvent.class));
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
Log.d("MON_TAG", account.toString());
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Toast.makeText(getApplicationContext(),"error while logging in",Toast.LENGTH_SHORT).show();
Log.w("errorMsg","Google sign in failed", e);
// ...
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {//acct means account
Log.d("MON_TAG", "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mFirebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d("MON_TAG", "signInWithCredential:success");
FirebaseUser user = mFirebaseAuth.getCurrentUser();
startActivity(new Intent(LoginActivity.this, CreateEvent.class));
Toast.makeText(getApplicationContext(),"You are now connected",Toast.LENGTH_SHORT).show();
} else {
GoogleSignInAccount account=GoogleSignIn.getLastSignedInAccount(getApplicationContext());
if(account !=null){
startActivity(new Intent(LoginActivity.this, CreateEvent.class));
}
// If sign in fails, display a message to the user.
Toast.makeText(getApplicationContext(),"Could not log in user",Toast.LENGTH_SHORT).show();
}
}
});
}