All Projects → pchmn → Rxsocialauth

pchmn / Rxsocialauth

Licence: apache-2.0
Android RxJava library for Social auth (Google, Facebook) and Smart Lock For Passwords

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Rxsocialauth

Socialloginmanager
DEPRECATED
Stars: ✭ 178 (+256%)
Mutual labels:  rxjava, oauth, facebook-login
Mern Boilerplate
Fullstack boilerplate with React, Redux, Express, Mongoose, Passport Local, JWT, Facebook and Google OAuth out of the box.
Stars: ✭ 112 (+124%)
Mutual labels:  oauth, facebook-login
Auth
:atom: Social (OAuth1\OAuth2\OpenID\OpenIDConnect) sign with PHP
Stars: ✭ 457 (+814%)
Mutual labels:  oauth, facebook-login
Billing Android
RxJava wrapper for Android Play Billing Library
Stars: ✭ 40 (-20%)
Mutual labels:  rxjava
Weaponapp
一个尽量做到极致的集大成App,努力做到最好(开发阶段)——MVVM+Retrofit+RxJava+Small 插件化+单元测试+MD
Stars: ✭ 1,011 (+1922%)
Mutual labels:  rxjava
Ethdroid
Easy-to-use Ethereum Geth wrapper for Android
Stars: ✭ 47 (-6%)
Mutual labels:  rxjava
Thirtyinch
a MVP library for Android favoring a stateful Presenter
Stars: ✭ 1,052 (+2004%)
Mutual labels:  rxjava
Soqlbuilder
Node.js and AngularJs based Query Builder for Salesforce using OAuth2 and REST API
Stars: ✭ 37 (-26%)
Mutual labels:  oauth
Meteorite
一个基于Android MVP的简单明了的指引性通用架构,目的是帮助更多的开发者去全面了解实践开发相关的各种技术,快速搭建属于自已的APP。这个项目涉及到如下技术的实际应用:1、MVP 2、网络请求(Novate基于rxjava,okhttp,retrofit封装架构)3、DbFlow(可保存文件入SD卡) 4、6.0权限申请 5、XRecyclerView 6、万能Adapter7、异常处理 8、日志打印 9、屏幕适配 10、代码混淆 11、多渠道打包 12、内存泄露检测 13、热修复 14、升级更新 15、极光推送 工程更新完善中……欢迎关注 @特别感谢ZJ.Y的Logo支持。
Stars: ✭ 49 (-2%)
Mutual labels:  rxjava
Starling Api Web Starter Kit
Starter kit and example app for using the Starling API.
Stars: ✭ 46 (-8%)
Mutual labels:  oauth
Graphql Retrofit Converter
A Retrofit 2 Converter.Factory for GraphQL.
Stars: ✭ 46 (-8%)
Mutual labels:  rxjava
Mvvm Kotlin Android Architecture
MVVM + Kotlin + Retrofit2 + Hilt + Coroutines + Kotlin Flow + mockK + Espresso + Junit5
Stars: ✭ 1,014 (+1928%)
Mutual labels:  rxjava
Bigbang
Android base project used by Xmartlabs team
Stars: ✭ 47 (-6%)
Mutual labels:  rxjava
Workmanger
laravel+vue.js 前后端分离实战项目(项目中wx端等已经存在,因为涉及业务较多,不再更新到github 需要参考可以私聊我)
Stars: ✭ 41 (-18%)
Mutual labels:  oauth
Wikibase Edit
a lib to edit Wikibase from NodeJS
Stars: ✭ 48 (-4%)
Mutual labels:  oauth
Oauth
(Deprecated) Helps developers consume OAuth-based web services in Craft plugins.
Stars: ✭ 38 (-24%)
Mutual labels:  oauth
Fxa Oauth Server
OAuth server for Firefox Accounts
Stars: ✭ 48 (-4%)
Mutual labels:  oauth
Nemiro.oauth.dll
Nemiro.OAuth is a class library for authorization via OAuth protocol in .NET Framework
Stars: ✭ 45 (-10%)
Mutual labels:  oauth
Socialite
Socialite is an OAuth2 Authentication tool. It is inspired by laravel/socialite, you can easily use it without Laravel.
Stars: ✭ 1,026 (+1952%)
Mutual labels:  oauth
Twittex
Twitter client library for Elixir.
Stars: ✭ 46 (-8%)
Mutual labels:  oauth

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.

Request credential

To retrieve user's stored credentials, use the requestCredential() method on a RxSmartLockPassword object. This method returns an Observable<CredentialRequestResult>.

// request credential
rxSmartLockPassword.requestCredential()
        .subscribe(credentialRequestResult -> {
            if (credentialRequestResult.getStatus().isSuccess()) {
                onCredentialRetrieved(credentialRequestResult.getCredential());
            } 
            else {
                resolveResult(credentialRequestResult.getStatus());
            }
            
        }, throwable -> {
            Log.e(TAG, throwable.getMessage());
        });
Request credential and auto sign in

To retrieve user's stored credentials and automatically sign in the user with found credentials, use the requestCredentialAndAutoSignIn() method. It will return an Observable<Object>.

With this method you don't have to handle different cases. No matter if there is no stored credential, if there is only one stored credential, or if there are multiple stored credentials, the requestCredentialAndAutoSignIn() method will do all the work.

  • If there is only one stored credential, or if the user picks one of the multiple stored credentials, this method will catch it :

    • If the account type is Google or Facebook, the user will be automatically sign in according to the provider, and the observer will receive a RxAccount object in case of success. If it fails a Throwable will be emitted.
    • If the account type is different from Google or Facebook the observer will receive a Credential object :
      • If the account type is null, this is a LoginPassword credential. In this case the Credential object will contain the id and the password, and you'll have to authenticate the user manually with the credential.
      • If the account type is not null, it means that there is a provider, but different from Google or Facebook, like Twitter for example
  • If the user cancels or if there is no stored credential, a Throwable will be emitted.

// request credential and auto sign in
rxSmartLockPasswords.requestCredentialAndAutoSignIn()
        .subscribe(o -> {
            if(o instanceof RxAccount) {
                // user is signed in using google or facebook
                RxAccount rxAccount = (RxAccount) o;
                Log.d(TAG, "provider: " + rxAccount.getProvider());
                Log.d(TAG, "email: " + rxAccount.getEmail());
             }

             else if(o instanceof Credential) {
                Credential credential = (Credential) o;
                if(credential.getAccountType() == null) {
                    // credential contains login and password
                    signInWithLoginPassword(credential.getId(), credential.getPassword());
                }
                else {
                    // credential from other provider than Google or Facebook
                    handleCredential(credential);
                }
             }

        }, throwable -> {
            Log.e(TAG, throwable.getMessage());
        });
        
        
private void signInWithLoginPassword(String login, String password) {
    // authenticate the user like you want
}

private void handleCredential(Credential credential) {
   // do your logic here
}

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.

To store a user's credentials, use the saveCredential(Credential credential) method on a RxSmartLockPassword object. It returns an Observable<RxStatus>.

If you used RxGoogleAuth or RxFacebookAuth with the enableSmartLock(true) option, the credential is stored automatically, and you don't have to use this method.

// save credential
rxSmartLockPassword.saveCredential(Credential credential)
        .subscribe(rxStatus -> {
            if(rxStatus.isSuccess()) {
                // credential saved
            }
            
        }, throwable -> {
            Log.e(TAG, throwable.getMessage());
        });

Delete stored credentials

To delete a stored credential, use the deleteCredential(Credential credential) method on a RxSmartLockPassword object. It returns an Observable<RxStatus>.

// delete credential
rxSmartLockPassword.deleteCredential(Credential credential)
        .subscribe(rxStatus -> {
            if(rxStatus.isSuccess()) {
                // credential deleted
            }
            
        }, throwable -> {
            Log.e(TAG, throwable.getMessage());
        });

Sample

A sample app with some use cases of the library is available on this link

Status

This library is currently in beta

Credits

This library was greatly inspired by SocialLoginManager

License

Copyright 2017 pchmn

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].