0

I'm implementing Google Sign by following Firebase offical docs. But the below codes do not show any google account when I click on the button to start google login.

Looks like the problem is with BeginSignInRequest.


 binding.btnGoogleSignIn.setOnClickListener {
            signInTest()
        }


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

        when (requestCode) {
            REQUEST_CODE_SIGN_IN -> {
                try {
                    val credential = Identity.getSignInClient(requireContext())
                        .getSignInCredentialFromIntent(data)
                    val idToken = credential.googleIdToken
                    when {
                        idToken != null -> {
                            // Got an ID token from Google. Use it to authenticate
                            // with Firebase.
                            Log.d(TAG, "Got ID token.")

                            val firebaseCredential = GoogleAuthProvider.getCredential(idToken, null)
                            auth.signInWithCredential(firebaseCredential)
                                .addOnCompleteListener() { task ->
                                    if (task.isSuccessful) {
                                        // Sign in success, update UI with the signed-in user's information
                                        Log.d(TAG, "signInWithCredential:success")
                                        val user = auth.currentUser
//                                        updateUI(user)
                                        findNavController().navigate(R.id.action_authFragment_to_homeFragment)
                                    } else {
                                        // If sign in fails, display a message to the user.
                                        Log.w(TAG, "signInWithCredential:failure", task.exception)

                                    }
                                }
                        }
                        else -> {
                            // Shouldn't happen.
                            Log.d(TAG, "No ID token!")
                        }
                    }
                } catch (e: ApiException) {
                    Log.d(TAG, "onActivityResult: ${e.message}")
                }
            }
        }

    }
 private fun signInTest() {
        BeginSignInRequest.builder()
            .setGoogleIdTokenRequestOptions(
                BeginSignInRequest.GoogleIdTokenRequestOptions.builder()
                    .setSupported(true)
                    // Your server's client ID, not your Android client ID.
                    .setServerClientId(getString(R.string.your_web_client_id))
                    // Only show accounts previously used to sign in.
                    .setFilterByAuthorizedAccounts(false)
                    .build()
            )
            .build()
    }

Shoaib Kakal
  • 1,090
  • 2
  • 16
  • 32
  • [OnActivityResult method is deprecated](https://stackoverflow.com/questions/62671106/onactivityresult-method-is-deprecated-what-is-the-alternative). So most likely you should the alternative solution. 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. Here is the corresponding [repo](https://github.com/alexmamo/FirebaseSignInWithGoogle). – Alex Mamo Sep 16 '22 at 05:22

0 Answers0