0

SO I have been trying to make Signin with google as a signin option for my app. As i click the sigin button, the app shows the list of availabe emails but as soon as i click any of the emails, the app doesnt log me in. I have done trying almost everything including, adding SHA-1 key to firebase, replacing the new google-services.json file. I have rechecked my code but i must be missing out on something here. Please help me find my mistakes

EDIT: I have enabled Sigin wit google in firebase too

Here is the code i used for the signin:

public class User_login extends AppCompatActivity {


    private static final int RC_SIGN_IN = 123;
    private FirebaseAuth mAuth;
    private GoogleSignInClient mGoogleSignInClient;



    @Override
    protected void onStart() {
        super.onStart();

        FirebaseUser user = mAuth.getCurrentUser();
        if(user != null){
            Intent intent = new Intent(getApplicationContext(), home_user.class);
            startActivity(intent);
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_login);

        mAuth = FirebaseAuth.getInstance();

    
    // ________________SIGN IN WITH GOOGLE _____________//

        createRequest();

        findViewById(R.id.google_signIn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                signIn();
            }
        });
    }

    private void createRequest() {
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
    }

    private void signIn() {
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

    @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(TAG, "firebaseAuthWithGoogle:" + account.getId());
                firebaseAuthWithGoogle(account.getIdToken());
            } catch (ApiException e) {
                // Google Sign In failed, update UI appropriately
                Log.w("DEBUG", "Google sign in failed", e);
                Toast.makeText(User_login.this,"Google signin failed",Toast.LENGTH_LONG).show();
            }
        }
    }

    private void firebaseAuthWithGoogle(String idToken) {
        AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
        mAuth.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("DEBUG", "signInWithCredential:success");

                            FirebaseUser user = mAuth.getCurrentUser();
                            Intent intent = new Intent(getApplicationContext(), home_user.class);
                            startActivity(intent);

                        } else {
                            Toast.makeText(User_login.this, "sorry something went wrong, please retry",Toast.LENGTH_LONG).show();
                            // If sign in fails, display a message to the user.
                            //Log.w(TAG, "signInWithCredential:failure", task.getException());

                        }
                    }
                });
    }



}

EDIT: Here's my logcat error:

2021-05-09 15:14:57.861 1997-1997/com.covoid.app W/DEBUG: Google sign in failed com.google.android.gms.common.api.ApiException: 10: at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(com.google.android.gms:play-services-base@@17.1.0:4) at com.google.android.gms.auth.api.signin.GoogleSignIn.getSignedInAccountFromIntent(com.google.android.gms:play-services-auth@@18.1.0:9) at com.covoid.app.User_login.onActivityResult(User_login.java:137) at android.app.Activity.dispatchActivityResult(Activity.java:8304) at android.app.ActivityThread.deliverResults(ActivityThread.java:5136) at android.app.ActivityThread.handleSendResult(ActivityThread.java:5184) at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2222) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:228) at android.app.ActivityThread.main(ActivityThread.java:7782) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:981)

Arpit
  • 3
  • 2
  • 9

1 Answers1

0

in Firebase check that you have to enabled google sign in ?

Go to firebase Authentication and inside sigin methods enable google sign in

Check the image

vikram
  • 84
  • 9