Last active
January 18, 2016 21:33
-
-
Save ppamorim/f3bc736460aa064c5a40 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import FBSDKCoreKit | |
import FBSDKLoginKit | |
class FacebookAuth { | |
class func auth(callback: (Bool) -> (), viewController: UIViewController) { | |
let parameters = ["fields" : "id,name,email"] | |
let facebookReadPermissions = ["public_profile", "email", "user_friends"] | |
if FBSDKAccessToken.currentAccessToken() != nil { | |
FBSDKAccessToken.refreshCurrentAccessToken { (connection, result, error) -> Void in | |
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: parameters) | |
graphRequest.startWithCompletionHandler { (connection, result, error) -> Void in | |
if error != nil { | |
FBSDKLoginManager().logOut() | |
callback(false) | |
} else if result != nil { | |
validateUserData(callback, result: result) | |
return | |
} else { | |
callback(false) | |
} | |
} | |
} | |
} | |
FBSDKLoginManager().logInWithReadPermissions(facebookReadPermissions, fromViewController: viewController, handler: { | |
(result:FBSDKLoginManagerLoginResult!, error:NSError!) -> Void in | |
if error != nil { | |
FBSDKLoginManager().logOut() | |
callback(false) | |
return | |
} else if result != nil && result.isCancelled { | |
FBSDKLoginManager().logOut() | |
callback(false) | |
return | |
} else { | |
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: parameters) | |
graphRequest.startWithCompletionHandler { (connection, result, error) -> Void in | |
if error != nil { | |
FBSDKLoginManager().logOut() | |
callback(false) | |
} else if result != nil { | |
validateUserData(callback, result: result) | |
} | |
} | |
} | |
}) | |
} | |
class func validateUserData(callback: (Bool) -> (), result : AnyObject?) { | |
if let userData = result as? [String:AnyObject] { | |
if userData.count > 0 { | |
analyseLogin(callback, | |
name: (userData["name"] as? String)!, | |
email: (userData["email"] as? String)!, | |
facebookId: (userData["id"] as? String)!) | |
} else { | |
callback(false) | |
} | |
} else { | |
callback(false) | |
} | |
} | |
class func analyseLogin(callback: (Bool) -> (), name: String, email: String, facebookId: String) { | |
//Request for our web service, no points to be verified here. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment