firebasekotlinfirebase-authenticationgoogle-signin

GetEmail() Null in Google Login with Firebase using Kotlin


I'm creating a login with email and pass (that works fine) plus the login with Google, the login works but neither in Firebase console nor in the code with 'account.email' can I see the email.

Firebase Console

binding.btGoogle.setOnClickListener() {
            val googleConfig: GoogleSignInOptions =
                GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestIdToken(getString(R.string.default_web_client_id)).build()
            val googleClient: GoogleSignInClient = GoogleSignIn.getClient(this, googleConfig)
            googleClient.signOut()
            startActivityForResult(googleClient.signInIntent, GOOGLE_SIGN_IN)
        }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == GOOGLE_SIGN_IN) {
            val task: Task<GoogleSignInAccount> = GoogleSignIn.getSignedInAccountFromIntent(data)
            try {
                val account: GoogleSignInAccount? = task.getResult(ApiException::class.java)
//here 'account.email' is already null
                if (account != null) {
                    val credential: AuthCredential = GoogleAuthProvider.getCredential(
                        account.idToken,
                        null
                    )

                    FirebaseAuth.getInstance().signInWithCredential(credential).addOnCompleteListener() {
                            if (it.isSuccessful) {
                                showHome(account.email ?: "", ProviderType.GOOGLE)
                            } else {
                                showAlert()
                            }
                        }
                }
            } catch (e: ApiException) {
                showAlert()
            }
        }
    }

In debugging, I can see other attributes like displayName, givenName, idToken... but email is just null.

Solutions I tried:

Something that bothers me:


Solution

  • I missed one word.

    I had this:

    val googleConfig: GoogleSignInOptions =
                    GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .build()
    

    And I needed this:

    val googleConfig: GoogleSignInOptions =
                    GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build()