Created
February 22, 2023 16:43
-
-
Save tdcolvin/89768850e773b0e6c574927dc81279f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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