Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use SmartLock for Passwords in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 553
    Comment on it

    SmartLock for passwords saves your favorites apps and websites accounts authentication to your google account for future use for fast Login.

    With SmartLock for passwords you save authentication in Android app may be available on Chrome on System. or you save authentication in Chrome may available in Android app.

     

    Steps to integrate SmartLock for Passwords in Android .

    1.  Add google play service dependency to gradle file.

    compile 'com.google.android.gms:play-services-auth:8.4.0'

    2. Implement GoogleApiClient.ConnectionCallbacks Interface to get callback.

    public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener

    2. Initialize GoogleApiClient.

    mCredentialsApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .enableAutoManage(this, this)
                    .addApi(Auth.CREDENTIALS_API)
                    .build();

    3.  Request for Credentials .

           // Request all of the user's saved username/password credentials.
            CredentialRequest request = new CredentialRequest.Builder()
                    .setSupportsPasswordLogin(true)
                    .build();
    
            Auth.CredentialsApi.request(mCredentialsApiClient, request).setResultCallback(
                    new ResultCallback<CredentialRequestResult>() {
                        @Override
                        public void onResult(CredentialRequestResult credentialRequestResult) {
                            Status status = credentialRequestResult.getStatus();
                            if (status.isSuccess()) {
                                // Successfully read the credential because there is only one credential saved.
                            
                            } else if (status.getStatusCode() == CommonStatusCodes.RESOLUTION_REQUIRED) {
                                 the user has multiple saved
                                // credentials and needs to pick one
                        
                            } else if (status.getStatusCode() == CommonStatusCodes.SIGN_IN_REQUIRED) {
                                Log.e(TAG,"SignIn Required");
                                
                            } else {
                                Log.w(TAG, "Unexpected status code: " + status.getStatusCode());
                            }
                        }
                    });
        }

    4. if status.isSuccess() that means only 1 authentication is saved. In this stage you can get username and passwords from credentialRequestResult.getCredential()

    5. If status is  CommonStatusCodes.RESOLUTION_REQUIRED that means there are more the one authentication saved. you need to select one from them .

      try {
                    status.startResolutionForResult(MainActivity.this, requestCode);
                    mIsResolving = true;
                } catch (IntentSender.SendIntentException e) {
                    Log.e(TAG, "STATUS: Failed to send resolution.", e);
                }

    6. You can get Credential instance at OnActivityResult. and get username and password from there.

     if (resultCode == RESULT_OK) {
                        Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
                    } else {
                        showToast("Credential Read Failed");
                    }

    7. if status.getStatusCode() == CommonStatusCodes.SIGN_IN_REQUIRED that means there is no credentials saved and you need to signin manually.

    8. When user first time use username and password to authenticate then save their authentication to google account too by below code.

    Credential credential = new Credential.Builder(emailid)
                    .setPassword(passwordString)
                    .build();
    
            Auth.CredentialsApi.save(mCredentialsApiClient, credential).setResultCallback(
                    new ResolvingResultCallbacks<Status>(this, RC_SAVE) {
                        @Override
                        public void onSuccess(Status status) {
                            showToast("Credential Saved");
                        }
    
                        @Override
                        public void onUnresolvableFailure(Status status) {
                            showToast("Credential Save Failed");
                        }
                    });


    Happy Coding :D

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: