import UIKit
import FBSDKLoginKit
import Firebase
class LoginViewController: UIViewController,UIScrollViewDelegate {
//MARK: VIEW LIFE CYCLES
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool)
{
super.viewDidAppear(animated)
}
//MARK: FACEBOOK LOGIN
//============
@IBAction func btnFBClick(_ sender: UIButton)
{
//calls the facebook login function that is defined in the fbMuser class
loginFaceBookuser()
self.dismiss(animated: true, completion: nil)
//MARK: facebook Login Function
//FACEBOOKElOGINFUNCTIONS
func loginFaceBookuser(){
let fbLoginManager : LoginManager = LoginManager()
fbLoginManager.logIn(permissions: ["email"], from: LoginViewController() )
{ (result, error) -> Void in
if (error == nil){
let fbloginresult : LoginManagerLoginResult = result!
// if user cancel the login
if (result?.isCancelled)!
{
return
}
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
//MARK: perform navigation segue to profile page
let homeView = self.storyboard?.instantiateViewController(withIdentifier: "profileviewcontroller") as! secondProfileTableViewController
self.navigationController?.pushViewController(homeView, animated: true)
// fbLoginManager.logOut()
}
}
}
}
//this function helps me get the user data from facebook then i then i store the all the data into firebase under my User collection
func getFBUserData()
{
if((AccessToken.current) != nil)
{
GraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler:
{ (connection, result, error) -> Void in
if (error == nil)
{
if let data = result as? NSDictionary
{
let firstName = data.object(forKey: "first_name") as? String
let lastName = data.object(forKey: "last_name") as? String
let name = data.object(forKey: "name") as? String
let id = data.object(forKey: "id") as? String
//what we need to do next is make a document path in the users collection for full address and purchase itemids
if let email = data.object(forKey: "email") as? String
{
//MARK: TO DO IF THE USERS ALREADY EXIST IN THE FIRESTORE SERVER DO NOT PERFORM SET DATA IT WILL OVERWRITE PREVIOUS DATA! NEED TO WRITE CODE TO DETECT THIS
FirebaseReferece(.User).document(id!).setData(data as! [String : Any]) //saves all the information under user the document id is id
saveit()
}else{
print("We are unable to access Facebook account details, please use other sign in methods.")
}
}
}
})
}
}
}//MARK: END OF CLASS