Google Smart Lock - How to Update Current Saved Credential Password?

414 Views Asked by At

I see that the CredentialsClient can save and delete a Credential but not update.

Do I need to delete and save the Credential again when trying to update the password in the App?

1

There are 1 best solutions below

0
Carlos Miguel Cuevas Ochoa On

No, just call save function and it will override the password.

for example here:

 val credentialsClient: CredentialsClient = Credentials.getClient(context)
        credentialsClient.save(credential).addOnCompleteListener { task -> onCredentialSaved(task) }

credential is an instance of com.google.android.gms.auth.api.credentials.Credential it contains an already saved credential but with new password.

and in the callback I am sending onCredentialSaved(task) wich is this:

      { task ->
                if (task.isSuccessful) {
                    Log.d(TAG, "SAVE: OK")
                    Toast.makeText(this, "Credentials saved", Toast.LENGTH_SHORT).show()
                    return@saveCredentials
                }

                val exception = task.exception
                if (exception is ResolvableApiException) {
                    // Try to resolve the save request. This will prompt the user if
                    // the credential is new.
                    val rae: ResolvableApiException? = exception as ResolvableApiException?
                    try {
                        rae?.startResolutionForResult(this, ON_SAVED_CREDENTIALS)
                    } catch (exception: SendIntentException) {
                        // Could not resolve the request
                        Log.e(
                            TAG,
                            "Failed to send resolution.",
                            exception
                        )
                        Toast.makeText(this, "Save failed", Toast.LENGTH_SHORT).show()
                    }
                } else {
                    // Request has no resolution
                    Toast.makeText(this, "Save failed", Toast.LENGTH_SHORT).show()
                }

            }

if you debug you will notice that when updating a credential it falls into the fist if clause because the task return as success, if it is a new credential It won't fall into that, and you have to handle it with the rest of the code.

I hope it helps you.