senty senty - 5 months ago 183
iOS Question

Firebase 3.x - Facebook Login

In Firebase 2.5.1, I used to do this, and it was working:

@IBAction func facebookLoginDidTouch(sender: AnyObject) {

let facebookLogin = FBSDKLoginManager()

facebookLogin.logInWithReadPermissions(["public_profile", "email"], fromViewController: self, handler: {
(facebookResult, facebookError) -> Void in
if facebookError != nil {
print("Facebook login failed. Error \(facebookError)")
} else if facebookResult.isCancelled {
print("Facebook login was cancelled.")
} else {
let accessToken = FBSDKAccessToken.currentAccessToken().tokenString
myRootRef.authWithOAuthProvider("facebook", token: accessToken, withCompletionBlock: { error, authData in

if error != nil {
print("Login failed. \(error)")
} else {
print("Logged in!")
let newUser = [
"provider": user.provider,
"imageUrl": user!.providerData["profileImageURL"] as String, // etc
]
}
})
}
})
}


Now I am trying to achieve this in Firebase 3.x. However, I got confused on where I should place permissions and stuff

This is what I tried so far

@IBAction func facebookLoginDidTouch(sender: AnyObject) {

let facebookLogin = FBSDKLoginManager()

let credential = FIRFacebookAuthProvider.credentialWithAccessToken(FBSDKAccessToken.currentAccessToken().tokenString)

FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in
if error != nil {
print("Login failed. \(error)")
} else {
print("Logged in!")

let newUser = [
"provider": user.provider,
"imageUrl": user!.providerData["profileImageURL"] as String
]
}
})
}


Update:

facebookLogin.logInWithReadPermissions(["public_profile", "email"], fromViewController: self, handler: {
(facebookResult, facebookError) -> Void in
if facebookError != nil {
print("Facebook login failed. Error \(facebookError)")
} else if facebookResult.isCancelled {
print("Facebook login was cancelled.")
} else {
// your firebase authentication stuff..

let credential = FIRFacebookAuthProvider.credentialWithAccessToken(FBSDKAccessToken.currentAccessToken().tokenString)

FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in
if error != nil {
print("Login failed. \(error)")
} else {
print("Logged in!")

let userID = Helpers.extractUID(user!)

let rootRef = FIRDatabase.database().reference()
let userRef = rootRef.child("users").child(userID)

userRef.observeEventType(.Value, withBlock: { snapshot in
if snapshot.value is NSNull {
let newUser = [
"providerId": user?.providerID,
"displayName": user?.displayName,
]
userRef.setValue((newUser as! AnyObject))
// perform segue
}
})
}
})

Answer

After you successfully login with facebook and get user data back ... you have to add firebase authentication stuff like

 @IBAction func facebookLoginDidTouch(sender: AnyObject) {

   let facebookLogin = FBSDKLoginManager() 

   facebookLogin.logInWithReadPermissions(["public_profile", "email"], fromViewController: self, handler: {
    (facebookResult, facebookError) -> Void in
        if facebookError != nil {
            print("Facebook login failed. Error \(facebookError)")
        } else if facebookResult.isCancelled {
            print("Facebook login was cancelled.")
        } else {
            // your firebase authentication stuff..

            let credential = FIRFacebookAuthProvider.credentialWithAccessToken(FBSDKAccessToken.currentAccessToken().tokenString)

            FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in
            if error != nil {
                  print("Login failed. \(error)")
            } else {
                 print("Logged in!")

            }
          })
        }
   })
}