Skip to content

sebastianmarschall/RxAuth

Repository files navigation

RxAuth

Reactive Auth APIs Wrapper Library for Google's Smart Lock for Passwords API.

API Android Arsenal Maven Central

Usage

Build RxAuth object

val rxAuth = RxAuth.Builder(context).setPasswordLoginSupported(true).setAccountTypes(IdentityProviders.GOOGLE).build()

Retrieve Credentials

rxAuth.retrieveCredentials().subscribe({
          // use Credential to sign in into your app
        }, {
            if (it is StatusException) {
                if (it.status.hasResolution()) {
                    try {
                        it.status.startResolutionForResult(activity, CREDENTIAL_REQUEST_RC)
                    } catch (e1: IntentSender.SendIntentException) {
                        Log.e(TAG, "RxAuth: Failed to send resolution.")
                    }

                } else {
                     // The user must create an account or sign in manually.
                     Log.e(TAG, "RxAuth: Unsuccessful credential request.");
                }
            }
        })

Watch for a StatusException in the onError() callback. When the user has stored more than one Credential Status.hasResolution() returns true. In this case call startResolutionForResult() to prompt the user to choose an account. You will get the chosen credentials in the activity's onActivityResult() method by calling the retrieveCredentialFromIntent(Intent) method on the RxAuth object that you created:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
   super.onActivityResult(requestCode, resultCode, data)
   if (requestCode == CREDENTIAL_REQUEST_RC) {
       if (resultCode == RESULT_OK) {
           rxAuth.retrieveCredentialFromIntent(data).subscribe({ credentials ->
               // use Credential to sign in into your app
           })
       }
   }
}

Store Credentials

rxAuth.storeCredentials(credential).subscribe({
  Log.d(TAG, "stored credentials")
}, {
  if (it is StatusException) {
      if (it.status.hasResolution()) {
          try {
              it.status.startResolutionForResult(activity, CREDENTIAL_STORE_RC)
          } catch (e1: IntentSender.SendIntentException) {
              Log.e(TAG, "STATUS: Failed to send resolution.")
          }

      }
  }
})

In case the credentials are new, the user must confirm the store request. Again watch for a StatusException in the onError() callback. Resolve the save request with startResolutionForResult() to prompt the user for confirmation. The activity's onActivityResult() notifies if the user confirmed the store request:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
   super.onActivityResult(requestCode, resultCode, data)
   if (requestCode == CREDENTIAL_STORE_RC) {
       if (resultCode == RESULT_OK) {
           Log.d(TAG, "RxAuth: user stored credentials")
       } else {
           Log.d(TAG, "RxAuth: user canceled storing credentials")
       }
   }
}

Delete Credentials

rxAuth.deleteCredential(credential).subscribe({
            if(it) {
               // Credential has been deleted
            }
        },{
            Log.e(TAG, "RxAuth: error while deleting credentials")
        })

Add RxAuth To Your Project

Add this to your build.gradle file:

dependencies {
     compile 'com.sebastianmarschall:rxauth:0.3.1'
}

Additional Notes

This library is strongly inspired by the RxSmartLock but is written in Kotlin and uses RxJava2.

License

Copyright 2017 Sebastian Marschall

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

Reactive Auth APIs Library for Android and RxJava 2

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published