0

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
 )
}
Alex Mamo
140k18 gold badges171 silver badges202 bronze badges
asked Apr 26, 2025 at 19:51
1
  • 1
    Signing in to Firebase with Google credentials is pretty well documented here with a full sample here. From looking there, you seem to be missing the GoogleIdTokenCredential.createFrom(credential.data) (and probably more, but that's what I spotted right away). --- What isn't working about the code you shared? Any error message? Commented Apr 26, 2025 at 20:44

1 Answer 1

1

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(/* ... /*)
}
answered Apr 27, 2025 at 4:20
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.