Store a user's credentials

  • Smart Lock for Passwords is deprecated; migrate to Credential Manager for continued security and usability.

  • After successful sign-in, account creation, or password change, store user credentials to automate future authentication.

  • To store credentials, create a Credential object and call CredentialsClient.save() .

  • If CredentialsClient.save() is not immediately successful, resolve the ResolvableApiException to prompt the user for confirmation.

  • Credentials saved using Smart Lock on Android O+ use the native autofill confirmation dialog and are shared with Autofill with Google.

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<Void> 
 () 
  
 { 
  
 @Override 
  
 public 
  
 void 
  
 onComplete 
 ( 
 @NonNull 
  
 Task<Void> 
  
 task 
 ) 
  
 { 
  
 if 
  
 ( 
 task 
 . 
 isSuccessful 
 ()) 
  
 { 
  
 Log 
 . 
 d 
 ( 
 TAG 
 , 
  
 "SAVE: OK" 
 ); 
  
 Toast 
 . 
 makeText 
 ( 
 activity 
 , 
  
 "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 
  
 exception 
 ) 
  
 { 
  
 // Could not resolve the request 
  
 Log 
 . 
 e 
 ( 
 TAG 
 , 
  
 "Failed to send resolution." 
 , 
  
 exception 
 ); 
  
 Toast 
 . 
 makeText 
 ( 
 activity 
 , 
  
 "Save failed" 
 , 
  
 Toast 
 . 
 LENGTH_SHORT 
 ). 
 show 
 (); 
  
 } 
  
 } 
  
 else 
  
 { 
  
 // Request has no resolution 
  
 Toast 
 . 
 makeText 
 ( 
 activity 
 , 
  
 "Save failed" 
 , 
  
 Toast 
 . 
 LENGTH_SHORT 
 ). 
 show 
 (); 
  
 } 
  
 } 
  
 }); 
< / 
 pre 
>  
 @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" 
 ); 
  
 } 
  
 } 
  
 // ... 
  
 } 
 

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.)

Design a Mobile Site
View Site in Mobile | Classic
Share by: