androidfirebasekotlinfirebase-authenticationandroid-credential-manager

Unresolved reference 'googleIdToken'


I am trying to set up Google sign-in using Firebase Authentication in Kotlin Jetpack. Somehow, I am not able to get the googleIdToken.

I have tried many ways still not able to solve. I tried using other methods, but they are all deprecated.

Row(
    horizontalArrangement = Arrangement.Center
) {
    val context = LocalContext.current

    // Initialize Credential Manager
    val credentialManager = CredentialManager.create(context)

    // Configure Credential Request
    val request = GetCredentialRequest.Builder()
        .addCredentialOption(
            GetGoogleIdOption.Builder()
                .setServerClientId(context.getString(R.string.default_web_client_id))
                .build()
        )
        .build()

    // Google Sign-In Icon
    Icon(
        painter = painterResource(id = R.drawable.google_search_logo),
        contentDescription = "Google Icon",
        modifier = Modifier
            .size(LocalConfiguration.current.screenWidthDp.dp * 0.075f)
            .clickable {
                CoroutineScope(Dispatchers.IO).launch {
                    try {
                        val response = credentialManager.getCredential(context, request)
                        val googleIdToken = response.googleIdToken // Adjusted to retrieve the token
                        if (googleIdToken != null) {
                            val firebaseCredential = GoogleAuthProvider.getCredential(googleIdToken, null)
                            FirebaseAuth.getInstance().signInWithCredential(firebaseCredential)
                                .addOnCompleteListener { task ->
                                    if (task.isSuccessful) {
                                        Log.d("SignIn", "Sign-in successful!")
                                        // Perform navigation or update UI
                                    } else {
                                        Log.e("SignIn", "Sign-in failed: ${task.exception}")
                                    }
                                }
                        } else {
                            Log.e("SignIn", "Google ID token is null")
                        }
                    } catch (e: Exception) {
                        Log.e("SignIn", "Credential Manager failed", e)
                    }
                }
            },
        tint = Color.Unspecified
    )
}

Solution

  • When you're using the following line of code:

    val response = credentialManager.getCredential(context, request)
    

    Please note that the "response" object is of type GetCredentialResponse. So, as you can see inside this class, there is no "googleIdToken" property present. There is, however, a credential property. So what you can do is to read this property:

    val credential = response.credential
    

    Perform the necessary check as mentioned in the docs, and right after that, get the GoogleIdTokenCredential object, which contains an getIdToken() function that returns the idToken that you're interested in. Lastly, you can sign in to Firebase:

    if (credential is CustomCredential && credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) {
        val googleIdTokenCredential = GoogleIdTokenCredential.createFrom(credential.data)
        val idToken = googleIdTokenCredential.idToken
        val authCredential = GoogleAuthProvider.getCredential(idToken, null)
        auth.signInWithCredential(authCredential).addOnCompleteListener(/* ... /*)
    }