Skip to content

Instantly share code, notes, and snippets.

@tdcolvin
Created February 22, 2023 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdcolvin/89768850e773b0e6c574927dc81279f4 to your computer and use it in GitHub Desktop.
Save tdcolvin/89768850e773b0e6c574927dc81279f4 to your computer and use it in GitHub Desktop.
private suspend fun getCredential(activity: Activity): PasswordCredential? {
try {
//Tell the credential library that we're only interested in password credentials
val getCredRequest = GetCredentialRequest(
listOf(GetPasswordOption())
)
//Show the user a dialog allowing them to pick a saved credential
val credentialResponse = credentialManager.getCredential(
request = getCredRequest,
activity = activity,
)
//Return the selected credential (as long as it's a username/password)
return credentialResponse.credential as? PasswordCredential
}
catch (e: GetCredentialCancellationException) {
//User cancelled the request. Return nothing
return null
}
catch (e: NoCredentialException) {
//We don't have a matching credential
return null
}
catch (e: GetCredentialException) {
Log.e("CredentialTest", "Error getting credential", e)
throw e
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment