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)
        .build();

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

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

Smart Lock Save dialog Then, call CredentialsApi.save() to save users' credentials. If the call to CredentialsApi.save() is not immediately successful, the credentials might be new, in which case the user must confirm the save request. Resolve the save request 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 CredentialsApi.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.

Auth.CredentialsApi.save(mCredentialsClient, credential).setResultCallback(
            new ResultCallback() {
                @Override
                public void onResult(Result result) {
                    Status status = result.getStatus();
                    if (status.isSuccess()) {
                        Log.d(TAG, "SAVE: OK");
                        Toast.makeText(this, "Credentials saved", Toast.LENGTH_SHORT).show();
                    } else {
                        if (status.hasResolution()) {
                            // Try to resolve the save request. This will prompt the user if
                            // the credential is new.
                            try {
                                status.startResolutionForResult(this, RC_SAVE);
                            } catch (IntentSender.SendIntentException e) {
                                // Could not resolve the request
                                Log.e(TAG, "STATUS: 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 CredentialsApi.request().