Skip to content

Instantly share code, notes, and snippets.

@mikekavouras
Last active July 12, 2016 18:10
Show Gist options
  • Save mikekavouras/d5a7a6eb0227b07b9ccd3143cc7351b7 to your computer and use it in GitHub Desktop.
Save mikekavouras/d5a7a6eb0227b07b9ccd3143cc7351b7 to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import UIKit
typealias JSON = [String: AnyObject]
protocol Authable {
static func login(params: JSON?, completion: (ResultType<User>) -> Void)
static func logout()
}
enum ResultType<T> {
case success(T)
case error(String)
}
struct User {
}
struct EmailAuth: Authable {
static func login(params: JSON?, completion: (ResultType<User>) -> Void) {
guard let params = params,
email = params["email"] as? String,
password = params["password"] as? String else {
completion(.error("enter a username and password"))
return
}
// do some login stuff
}
static func logout() {
// remove stuff
}
}
struct FacebookAuth: Authable {
static func login(params: JSON? = nil, completion: (ResultType<User>) -> Void) {
// do the fb auth flow
}
static func logout() {
// remove stuff
}
}
enum Auth {
case email(String, String)
case facebook
func login(completion: (ResultType<User>) -> Void) {
switch self {
case .email(let email, let password):
EmailAuth.login(["email":email, "password":password], completion: completion)
case .facebook:
FacebookAuth.login(completion: completion)
}
}
}
Auth.email("", "").login { result in
}
Auth.facebook.login { result in
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment