I am trying to add google sign in using Firebase in my Kotlin jetpack compose. But it is giving me the issue - No Credentials available. I want to show the list of google accounts and then one can select the account and it will show the toast success. I tried different ways still the same issue pops up.
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))
.setFilterByAuthorizedAccounts(true)
.setAutoSelectEnabled(true)
.build()
)
.build()
var showToast by remember { mutableStateOf(false) }
var showFailureToast by remember { mutableStateOf(false) }
// Google Sign-In Icon
Icon(
painter = painterResource(id = R.drawable.google_search_logo),
contentDescription = "Google Icon",
modifier = Modifier
.size(windowWidthFraction(0.075f))
.clickable {
CoroutineScope(Dispatchers.IO).launch {
try {
val response = credentialManager.getCredential(context, request)
val credential = response.credential
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)
FirebaseAuth.getInstance().signInWithCredential(authCredential).addOnCompleteListener{ task ->
if (task.isSuccessful) {
Log.d("SignIn", "Sign-in successful!")
showToast = true
}
}
}
} catch (e: NoCredentialException) {
// Silent sign-in failed, try with user interaction
try {
val interactiveRequest = GetCredentialRequest.Builder()
.addCredentialOption(
GetGoogleIdOption.Builder()
.setServerClientId(context.getString(R.string.default_web_client_id))
.setFilterByAuthorizedAccounts(false) // Allow account picker
.setAutoSelectEnabled(true)
.build()
)
.build()
val response = credentialManager.getCredential(context, interactiveRequest)
handleGoogleCredential(response.credential) {
showToast = true
}
} catch (ex: Exception) {
Log.e("SignIn", "Interactive sign-in failed", ex)
showFailureToast = true
}
} catch (e: Exception) {
Log.e("SignIn", "Unexpected error during sign-in", e)
showFailureToast = true
}
}
},
tint = Color.Unspecified
)
}
Use SHA1 instead of SHA256. I don't know why it works. But this solved my issue.