0

I am using this type of codes.it is not working,

I run app in debug mode.

It stop in below lines of code.

The code lines are didn't run so it was not login

I am trying to login with the google login it was working up to the OnActivityResult (if(resultcode == Activity.RESULT_OK).

this code was referred to in Firebase Documentation.

    fireBaseAuth = FirebaseAuth.getInstance()
        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.client_ID))
            .requestEmail()
            .build()
        googleSignINClient = GoogleSignIn.getClient(this@SignInActivity,gso)

        googleSignin = findViewById(R.id.google_login)
        googleSignin.setOnClickListener {
            signIn()
        }

.setOnClickListener {
            startActivity(Intent(this,SignUpActivity::class.java))
        }

private fun signIn()
    {
        val signInIntent = googleSignINClient!!.signInIntent
        startActivityForResult(signInIntent,RC_SIGN_IN)
    }

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == RC_SIGN_IN)
        {
            if (resultCode == Activity.RESULT_OK)
            {
                val task = GoogleSignIn.getSignedInAccountFromIntent(data)
                try {
                    val account = task.getResult(ApiException::class.java)
                    firebaseAuthWithGoogle(account!!)
                }catch (e : ApiException)
                {
                    Log.e(TAG, "Google SignIn failed $e")

                    Toast.makeText(this@SignInActivity, "Google SignIn failed",Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

 private fun firebaseAuthWithGoogle(account: GoogleSignInAccount){
        val credential = GoogleAuthProvider.getCredential(account.idToken, null)

        fireBaseAuth!!.signInWithCredential(credential)
            .addOnCompleteListener(this@SignInActivity) { task ->

                if (task.isSuccessful){
                    startActivity(Intent(this@SignInActivity, MainActivity::class.java))
                    finish()
                }
                else
                {
                    Toast.makeText(this@SignInActivity, "Authentication Failed",Toast.LENGTH_SHORT).show()
                }
            }

logcat snap. enter image description here

aswanth gs
  • 17
  • 6
  • Could you be a little more specific than "it is not working"? Provide logcat or a stacktrace, please. – Martin Zeitler Jun 04 '22 at 11:03
  • [OnActivityResult method is deprecated](https://stackoverflow.com/questions/62671106/onactivityresult-method-is-deprecated-what-is-the-alternative). You should consider using `registerForActivityResult` instead. Since you're using Kotlin, I think that this [resource](https://medium.com/firebase-developers/how-to-authenticate-to-firebase-using-google-one-tap-in-jetpack-compose-60b30e621d0d) will help. – Alex Mamo Jun 05 '22 at 07:41

0 Answers0