Skip to content

Instantly share code, notes, and snippets.

@tdcolvin
Last active February 22, 2023 16:37
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/8345624ead7ded5a134962e94a66d385 to your computer and use it in GitHub Desktop.
Save tdcolvin/8345624ead7ded5a134962e94a66d385 to your computer and use it in GitHub Desktop.
//Typically you would run this function only after a successful sign-in. No point in saving
//credentials that aren't correct.
private suspend fun saveCredential(activity: Activity, username: String, password: String) {
try {
//Ask the user for permission to add the credentials to their store
credentialManager.createCredential(
request = CreatePasswordRequest(username, password),
activity = activity,
)
Log.v("CredentialTest", "Credentials successfully added")
}
catch (e: CreateCredentialCancellationException) {
//do nothing, the user chose not to save the credential
Log.v("CredentialTest", "User cancelled the save")
}
catch (e: CreateCredentialException) {
Log.v("CredentialTest", "Credential save error", e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment