Store a user's credentials

After users successfully sign in, create accounts, or change passwords, allow them to store their credentials to automate future authentication in your app.

Before you begin

Configure an Android Studio project.

Store credentials

Create a Credential object containing a user's sign-in information. For example, to let users store their credentials after successfully signing in with their passwords:

Credential credential = new Credential.Builder(email)
        .setPassword(password)  // Important: only store passwords in this field.
                                // Android autofill uses this value to complete
                                // sign-in forms, so repurposing this field will
                                // likely cause errors.
        .build();

Or, for example, after users successfully sign in with their Google account:

GoogleSignInAccount gsa = signInTask.getResult();
Credential credential = new Credential.Builder(gsa.getEmail())
        .setAccountType(IdentityProviders.GOOGLE)
        .setName(gsa.getDisplayName())
        .setProfilePictureUri(gsa.getPhotoUrl())
        .build();

Smart Lock Save dialog Then, call CredentialsClient.save() to save users' credentials. If the call to CredentialsClient.save() is not immediately successful, the credentials might be new, in which case the user must confirm the save request. Resolve the ResolvableApiException with startResolutionForResult() to prompt the user for confirmation.

If the user chooses not to save credentials, the user won't be prompted again to save any account's credentials for the app. If you call CredentialsClient.save() after a user has opted out, its result will have a status code of CANCELED. The user can opt in later from the Google Settings app, in the Smart Lock for Passwords section. The user must enable credential saving for all accounts to be prompted to save credentials next time.

mCredentialsClient.save(credential).addOnCompleteListener(
        new OnCompleteListener() {
            @Override
            public void onComplete(@NonNull Task task) {
                if (task.isSuccessful()) {
                    Log.d(TAG, "SAVE: OK");
                    Toast.makeText(this, "Credentials saved", Toast.LENGTH_SHORT).show();
                    return;
                }

                Exception e = task.getException();
                if (e instanceof ResolvableApiException) {
                    // Try to resolve the save request. This will prompt the user if
                    // the credential is new.
                    ResolvableApiException rae = (ResolvableApiException) e;
                    try {
                        rae.startResolutionForResult(this, RC_SAVE);
                    } catch (IntentSender.SendIntentException e) {
                        // Could not resolve the request
                        Log.e(TAG, "Failed to send resolution.", e);
                        Toast.makeText(this, "Save failed", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    // Request has no resolution
                    Toast.makeText(this, "Save failed", Toast.LENGTH_SHORT).show();
                }
            }
        });
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // ...

    if (requestCode == RC_SAVE) {
        if (resultCode == RESULT_OK) {
            Log.d(TAG, "SAVE: OK");
            Toast.makeText(this, "Credentials saved", Toast.LENGTH_SHORT).show();
        } else {
            Log.e(TAG, "SAVE: Canceled by user", e);
        }
    }

    // ...

}

After storing credentials, retrieve them by calling CredentialsClient.request().

Targeting Android O and above

When you save password credentials using Smart Lock on devices running Android O or newer, Smart Lock uses the native autofill confirmation dialog over its own dialog whenever possible. (Note that credentials saved using Autofill with Google are bi-directionally shared with Smart Lock for Passwords.)

To force the display the Smart Lock for Passwords save dialog:

  1. Enable the save confirmation dialog for all platforms via the CredentialsOptions when building the CredentialsClient.

    CredentialsOptions options = new CredentialsOptions.Builder()
            .forceEnableSaveDialog()
            .build();
    mCredentialsClient = Credentials.getClient(this, options);
    
  2. Suppress the Autofill save dialog by marking the root view associated with the login fields as not important for Autofill:

    <!-- Mark the root view with android:importantForAutofill="noExcludeDescendants" -->
    <LinearLayout
        android:importantForAutofill="noExcludeDescendants"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <!-- ... -->
    </LinearLayout>