Add password-only snippet for credential manager#869
Conversation
|
Here is the summary of changes. You are about to add 3 region tags.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request introduces password sign-in functionality using the Android Credential Manager API by adding the signInWithPassword and handlePasswordSignIn functions. The review feedback correctly identifies a performance issue where runBlocking is used on the Android main thread, which can lead to ANR errors. It is recommended to refactor the implementation to use a suspending function and to include proper error logging in the catch block.
| fun signInWithPassword() { | ||
| // [START android_identity_password_get_request] | ||
| val getPasswordOption = GetPasswordOption() | ||
|
|
||
| val credentialRequest = GetCredentialRequest.Builder() | ||
| .addCredentialOption(getPasswordOption) | ||
| .build() | ||
| // [END android_identity_password_get_request] | ||
|
|
||
| runBlocking { | ||
| // [START android_identity_password_get_credential] | ||
| coroutineScope { | ||
| try { | ||
| val result = credentialManager.getCredential( | ||
| context = activityContext, | ||
| request = credentialRequest | ||
| ) | ||
| handlePasswordSignIn(result) | ||
| } catch (e: GetCredentialException) { | ||
| // Handle failure | ||
| } | ||
| } | ||
| // [END android_identity_password_get_credential] | ||
| } | ||
| } |
There was a problem hiding this comment.
Using runBlocking in Android is generally discouraged as it blocks the current thread (often the main UI thread) until the coroutine completes, which can lead to Application Not Responding (ANR) errors. Since credentialManager.getCredential is a suspending function, signInWithPassword should be declared as a suspend function. This allows the caller to manage the coroutine scope appropriately (e.g., using lifecycleScope or viewModelScope). Additionally, the empty catch block should at least log the exception to assist with debugging.
suspend fun signInWithPassword() {
// [START android_identity_password_get_request]
val getPasswordOption = GetPasswordOption()
val credentialRequest = GetCredentialRequest.Builder()
.addCredentialOption(getPasswordOption)
.build()
// [END android_identity_password_get_request]
// [START android_identity_password_get_credential]
coroutineScope {
try {
val result = credentialManager.getCredential(
context = activityContext,
request = credentialRequest
)
handlePasswordSignIn(result)
} catch (e: GetCredentialException) {
Log.e("CredentialManager", "Password sign-in failed", e)
}
}
// [END android_identity_password_get_credential]
}There was a problem hiding this comment.
Not included in actual code snippet tags
|
@yrezgui @kkuan2011 the build seems to be failing for reasons outside of the functions I added. Gemini says the github workflow may need to be updated - can you TAL? |
No description provided.