Home

Awesome

Version License Platform Language

OAuth RxSwift extension for iOS. Android version is located at this repository.

RxSocialConnect-iOS

RxSocialConnect simplifies the process of retrieving authorizations tokens from multiple social networks to a minimalist observable call, from any ViewController

let facebookApi20: FacebookApi20 = // ...
        
RxSocialConnect.with(self, providerOAuth20: facebookApi20)
            .subscribeNext { credential in self.showAlert(credential.oauth_token) }

Features

Setup

Add RxSocialConnect-iOS to your podfile

pod 'RxSocialConnect'

It also has a module which works with Moya (To configure OAuth headers in the endpointClosure)

pod 'RxSocialConnect/Moya'

Usage

Retrieving tokens using OAuth1

On social networks which use OAuth1 protocol to authenticate users (such us Twitter), you need to build an instance of an object which inherits from ProviderOAuth1 and pass it to RxSocialConnect.

let twitterApi = TwitterApi(
            consumerKey: consumerKey,
            consumerSecret: consumerSecret,
            callbackUrl: callbackURL
        )
        
RxSocialConnect.with(self, providerOAuth1: twitterApi)
            .subscribeNext { credential in self.showAlert(credential.oauth_token) }

Retrieving tokens using OAuth2

On social networks which use OAuth2 protocol to authenticate users (such us Facebook, Google+ or LinkedIn), you need to build an instance of an object which inherits from ProviderOAuth1 and pass it to RxSocialConnect.

let facebookApi20 = FacebookApi20(
            consumerKey: consumerKey,
            consumerSecret: consumerSecret,
            callbackUrl: callbackURL,
            scope: "public_profile"
        )
        
RxSocialConnect.with(self, providerOAuth20: facebookApi20)
            .subscribeNext { credential in self.showAlert(credential.oauth_token) }

Token lifetime

After retrieving the token, RxSocialConnect will save it on disk to return it on future calls without doing again the oauth process. This token only will be evicted from cache if its expiration time has been fulfilled.

But, if you need to close an specific connection (or delete the token from the disk for that matters), you can call RxSocialConnect.closeConnection(baseApiClass) at any time to evict the cached token -where baseApiClass is the provider class used on the oauth process

// Facebook
RxSocialConnect.closeConnection(FacebookApi20.self)
	.subscribeNext { self.showAlert("Facebook disconnected") }
// Twitter
RxSocialConnect.closeConnection(TwitterApi.self)
	.subscribeNext { self.showAlert("Twitter disconnected") }

You can also close all the connections at once, calling RxSocialConnect.closeConnections()

RxSocialConnect.closeConnections()
	.subscribeNext { self.showAlert("All disconnected") }

Moya

Its really easy, the only thing you need to do is to call this method and it will add the OAuth headers to your endpoint:

RxSocialConnect.addOAuthHeaders(/*ProviderOAuth 1 or 20 */, endpoint: endpoint)

Here is an example using FacebookApi20 provider:

// MARK: - Endpoint Closure
let endpointClosure = { (target: Target) -> Endpoint<Target> in
    let endpoint: Endpoint<Target> = Endpoint<Target>(URL: url(target), sampleResponseClosure: {.NetworkResponse(200, target.sampleData)}, method: target.method, parameters: target.parameters, parameterEncoding: target.parameterEncoding)
    // Add this line to add OAuthHeaders
    return RxSocialConnect.addOAuthHeaders(FacebookApi20.self, endpoint: endpoint)
}

Credits

Author

Roberto Frontado

Another author's libraries using RxSwift: