Skip to content

Add password-only snippet for credential manager#869

Merged
cy245 merged 3 commits intomainfrom
identity_add_password_snippets
Apr 16, 2026
Merged

Add password-only snippet for credential manager#869
cy245 merged 3 commits intomainfrom
identity_add_password_snippets

Conversation

@cy245
Copy link
Copy Markdown
Contributor

@cy245 cy245 commented Apr 15, 2026

No description provided.

@cy245 cy245 requested review from kkuan2011 and yrezgui as code owners April 15, 2026 20:34
@snippet-bot
Copy link
Copy Markdown

snippet-bot bot commented Apr 15, 2026

Here is the summary of changes.

You are about to add 3 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@cy245 cy245 requested a review from vinishavathwani April 15, 2026 20:34
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +139 to +163
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]
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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]
    }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not included in actual code snippet tags

@cy245
Copy link
Copy Markdown
Contributor Author

cy245 commented Apr 15, 2026

@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?

@cy245 cy245 merged commit b99814d into main Apr 16, 2026
6 checks passed
@cy245 cy245 deleted the identity_add_password_snippets branch April 16, 2026 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants