SkillAgentSearch skills...

RxSocialAuth

Android RxJava library for Social auth (Google, Facebook) and Smart Lock For Passwords

Install / Use

/learn @pchmn/RxSocialAuth
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

RxSocialAuth

Android RxJava library for Social auth (Google, Facebook) and Smart Lock For Passwords

Release

Setup

To use this library your minSdkVersion must be >= 15.

In your project level build.gradle :

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

In your app level build.gradle :

dependencies {
    compile 'com.github.pchmn:RxSocialAuth:2.0.1-beta'
}

Usage

Getting started

In order to use this library with Google Sign-In and Facebook Login, you need to prepare your project.

Google Sign-In

Follow the official guide : https://developers.google.com/identity/sign-in/android/start-integrating

In short :

  • Put your google-services.json into the /app directory of your project
  • Add the dependency classpath 'com.google.gms:google-services:3.0.0' to your project-level build.gradle
  • Add the plugin apply plugin: 'com.google.gms.google-services' at the end of your app-level build.gradle
  • Add the dependency compile 'com.google.android.gms:play-services-auth:10.0.1' to your app-level build.gradle

Facebook Login

  • Create an app ID on the facebook developer website : https://developers.facebook.com/ and activate Facebook Login on the console administration.
  • Add your facebook app ID in the strings.xml (or keys.xml) of your project like this :
<string name="facebook_app_id">you_app_id</string>
  • Modify your Manifest.xml like this :
<application android:label="@string/app_name" ...>
    ...
    <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id" />
    ...
</application>
  • You don't have to add the facebook SDK in your project, it is already included in this library

If you have trouble when trying to authenticate users, maybe you have to add the users you're trying to authenticate in your test users. See http://stackoverflow.com/questions/41861564/server-error-code-1675030-message-error-performing-query

Shared classes

RxAccount

This class represents a social account and has these methods :

  • String getProvider()
  • String getId()
  • String getAccessToken()
  • String getEmail()
  • String getFirstname()
  • String getLastname()
  • String getDisplayName()
  • Uri getPhotoUri()

These methods can return a null object according to the permissions you asked or didn't ask when you signed in the user.

RxStatus

This class represents the status of a request and has these methods :

  • int getStatusCode()
  • String getMessage()
  • boolean isSuccess()
  • boolean isCanceled()

Google Sign-In

Create a RxGoogleAuth object using the RxGoogleAuth.Builder builder.

// build RxGoogleAuth object
// 'this' represents a FragmentActivity
RxGoogleAuth rxGoogleAuth = new RxGoogleAuth.Builder(this)
        .requestIdToken(getString(R.string.oauth_google_key))
        .requestEmail()
        .requestProfile()
        .disableAutoSignIn()
        .enableSmartLock(true)
        .build();

You can configure the builder with these methods :

  • requestEmail() : Request the email
  • requestProfile() : Request the profile (mandatory to get the profile picture)
  • requestIdToken(String clientId) : Request an id token for authenticated users (mandatory to get the profile picture)
  • disableAutoSignIn() : Clear the account previously selected by the user, so the user will have to pick an account
  • enableSmartLock(boolean enable) : Enable or disable Smart Lock For Passwords. If enabled, it will save automatically the credential in Smart Lock For Passwords

Sign in and silent sign in

signIn() and silentSignIn(Credential credential) methods return an Observable<RxAccount>.

I think I found a bug in the Google Sign-In API with the silentSignIn() method. If the user you try to silent sign in doesn't have a Google+ account, no matter if you set the requestProfile() options, you won't have his profile.

// sign in
rxGoogleAuth.signIn()
        .subscribe(rxAccount -> {
            // user is signed in
            // use the rxAccount object as you want
            Log.d(TAG, "name: " + rxAccount.getDisplayName());
            Log.d(TAG, "email: " + rxAccount.getEmail());
            
        }, throwable -> {
            Log.e(TAG, throwable.getMessage());
        });


// silent sign in
// you have to pass a credential object in order to silent sign in
// if it fails the credential will be deleted from smart lock for passwords
rxGoogleAuth.silentSignIn(Credential credential)
        .subscribe(rxAccount -> {
            // user is signed in
            // use the rxAccount object as you want
            Log.d(TAG, "name: " + rxAccount.getDisplayName());
            Log.d(TAG, "email: " + rxAccount.getEmail());
            
        }, throwable -> {
            Log.e(TAG, throwable.getMessage());
        });

Sign out and revoke access

signOut() and revokeAccess() methods return an Observable<RxStatus>.

// sign out
rxGoogleAuth.signOut()
        .subscribe(rxStatus -> {
            if(rxStatus.isSuccess()) {
                // user is signed out
            }
            
        }, throwable -> {
            Log.e(TAG, throwable.getMessage());
        });
       
       
// revoke access
rxGoogleAuth.revokeAccess()
        .subscribe(rxStatus -> {
            if(rxStatus.isSuccess()) {
                // access is revoked
            }
            
        }, throwable -> {
            Log.e(TAG, throwable.getMessage());
        }); 

Facebook Login

Create a RxFacebookAuth object using the RxFacebookAuth.Builder builder.

// build RxFacebookAuth object
// 'this' represents a FragmentActivity
RxFacebookAuth rxFacebookAuth = new RxFacebookAuth.Builder(this)
        .enableSmartLock(true)
        .requestEmail()
        .requestProfile()
        .requestPhotoSize(200, 200)
        .build();

You can configure the builder with these methods :

  • requestEmail() : Request the email
  • requestProfile() : Request the profile (mandatory to get the profile picture)
  • requestPhotoSize(int width, int height) : Request a specific photo size, cause request profile
  • enableSmartLock(boolean enable) : Enable or disable Smart Lock For Passwords. If enabled, it will save automatically the credential in Smart Lock For Passwords

Sign in

Like with Google Sign-In, the signIn() method will return an Observable<RxAccount>.

// sign in
rxFacebookAuth.signIn()
        .subscribe(rxAccount -> {
            // user is signed in
            // use the rxAccount object as you want
            Log.d(TAG, "name: " + rxAccount.getDisplayName());
            Log.d(TAG, "email: " + rxAccount.getEmail());
            
        }, throwable -> {
            Log.e(TAG, throwable.getMessage());
        });

Sign out

signOut() method returns an Observable<RxStatus>.

// sign out
rxFacebookAuth.signOut()
        .subscribe(rxStatus -> {
            if(rxStatus.isSuccess()) {
                // user is signed out
            }
            
        }, throwable -> {
            Log.e(TAG, throwable.getMessage());
        });

RxAuthSocial

This class permits to access and sign out the current account without knowing if it is a Google account or a Facebook account. For example it is useful when you want to sign out the current user but you don't know if he/she is connected with a Google or a Facebook account.

This class uses the singleton pattern.

Sign out

This method will signed out the current user from the app, and from the current provider. It will also disable auto sign in for Smart Lock for Passwords and for the current provider. So the user will be able to pick an other account when using the authentication method again.

// sign out
// 'this' represents a FragmentActivity
RxSocialAuth.getInstance().signOut(this)
        .subscribe(rxStatus -> {
            if(rxStatus.isSuccess()) {
                // user is signed out
            }
            
            }, throwable -> {
                    Log.e(TAG, throwable.getMessage());
            });

Get current user

This method return a RxAccount object representing the current user in the app. If the user is signed out, the RxAccount object will be null.

// current user
RxAccount currentUser = RxSocialAuth.getInstance().getCurrentUser();

Smart Lock For Passwords

Create a RxSmartLockPasswords object using the RxSmartLockPasswords.Builder builder.

// build RxSmartLockPassword object
// 'this' represents a FragmentActivity
RxSmartLockPasswords rxSmartLockPasswords = new RxSmartLockPasswords.Builder(this)
        .disableAutoSignIn()
        .setAccountTypes(IdentityProviders.GOOGLE, IdentityProviders.FACEBOOK)
        .build()

You can configure the builder with these methods :

  • setAccountTypes(String... accountTypes) : Set the account types (identity providers)
  • setPasswordLoginSupported(boolean supported) : Enable returning credentials with a password, that is verified by the application
  • disableAutoSignIn() : Disable auto sign

Retrieve a user's stored credentials

Automatically sign users into your app by using the Credentials API to request and retrieve stored credentials for your users.

If you want to retrieve a Facebook account credential, for example, you have to call .setAccountTypes(IdentityProviders.FACEBOOK) on the builder of your RxSmartLockPassword object before.

View on GitHub
GitHub Stars52
CategoryDevelopment
Updated2y ago
Forks8

Languages

Java

Security Score

85/100

Audited on Jan 19, 2024

No findings