Giter Club home page Giter Club logo

twiauth's Introduction

๐Ÿฆ‰ TwiAuth ๐Ÿ’ซ

Twitter OAuth made simple for iOS using ASWebAuthenticationSession. ๐Ÿ•ต๐Ÿฝโ€โ™€๏ธ

Installing in your Xcode Project

  • Head to your scheme on Xcode and select Swift Package Manger.
  • Copy/Paste the URL of the TwiAuth repo and proceed to install the latest version or master, as desire.

Usage

Configuration & Setup

Let's see how it fits with your ViewController.

class ViewController: UIViewController {
	let twiAuth
	init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
		  let config = CredentialsConfig(
            consumerKey: "consumer-key",
            consumerSecret: "consumer-secret",
            callbackScheme: "callback-scheme"
        )
			twiAuth = TwiAuth(config: config)
			super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
	}
}

This is how it looks as base setup. But this isn't functional yet. Since TwiAuth uses ASWebAuthenticationSession under the hood, it requires the authorization the user via its own view controller that needs to be presented on a provided window. Let's look at how we can configure ASWebAuthenticationPresentationContextProviding.

class ViewController: UIViewController {
	let twiAuth
	init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
			....
			super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
			// Sets the presentation context.
			twiAuth.presentationContextProviding = self
	}
}
	
extention ViewController: ASWebAuthenticationPresentationContextProviding {
	    func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
        return view.window!
    }
}

Token Providing

TwitAuth comes with a handy TwiAuthTokenProviding protocol for convenience to write and read token wherever you app decides to store them.

This convenience would allow TwiAuth to read an existing token and respond back instead to generating a new one. Let' see how we can confirm to this.

class ViewController: UIViewController {
	let twiAuth
	init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
			....
			super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
			// TwiAuthTokenProviding conformance.
			twiAuth.tokenProviding = self
	}
}

extension ViewController: TwiAuthTokenProviding {
    func read() -> AccessToken? {
        try? keychain.retrieve()
    }

    func write(token: AccessToken) {
        do {
            try keychain.save(token: token)
        } catch {
            debugPrint("error while saving token: \(error)")
        }
    }
}

Authorizing your Requests

TwitAuth to provide the authorization header String so your client can directly sign the request themselves.

let oauthHeader = twiAuth.accessTokenAuthHeader(url: url, method: method)

var request = URLRequest(url: endpoint.url)
request.addValue(oauthHeader, forHTTPHeaderField: "Authorization")

โœจ Happy Hacking with Twitter Api! ๐Ÿ‘จ๐Ÿฝโ€๐Ÿ’ป ๐Ÿ‘จ๐Ÿฝโ€๐ŸŽจ

twiauth's People

Contributors

iamraafay avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.