I've a question to people who use Firebase Google Sign-In authentication:
My app, which has already been working for a couple of months, is using both Google Sign-In and e-mail/password options for Firebase Authentication. However about a week ago I've noticed that the Google Sign-In stopped working. No code was changed, also the e-mail/password option works just as usual.
I've checked the documentation (https://firebase.google.com/docs/auth/android/google-signin), it's still the same (My app copies the authentication method from the documentation).
Did you face a similar problem too? If yes, please tell me how can I solve it.
Here is my code related to Google Sign-in inside my LoginActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
...
//--------Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(LoginActivity.this, "Connection failed!", Toast.LENGTH_SHORT).show();
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
mGoogleBttn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SignIn();
}
});
}
private void SignIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mProgress.setMessage("Signing in with Google Account...");
mProgress.show();
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
// Google Sign In failed, update UI appropriately
// ...
mProgress.dismiss();
Toast.makeText(LoginActivity.this, "Google sign in failed!", Toast.LENGTH_SHORT).show();
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
mProgress.dismiss();
checkUserExist();
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
private void checkUserExist() {
final String user_id = mAuth.getCurrentUser().getUid();
mDatabaseUsers.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(user_id)) {
Intent mainIntent = new Intent(LoginActivity.this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mainIntent);
} else {
Intent setupIntent = new Intent(LoginActivity.this, SetupActivity.class);
setupIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(setupIntent);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Here are my app and project level build.gradle files: https://gist.github.com/anonymous/2c9737e898ca2568129af63e61a30f16
result.getStatusCode(), and then check the status code to determine the cause. Also, while an unlikely cause, have you checked to make sure that Google Sign In is toggled on in the Firebase console? - Jen Person