0

I'm trying to implement Facebook login with my Swift app. I was able to login to the app just fine. However, I ran into an issue once I logged the user out. I get the error the supplied Facebook session token is expired or invalid. How can I generate a new token? Here's the code:

      if let accessToken: FBSDKAccessToken = FBSDKAccessToken.currentAccessToken() {
        PFFacebookUtils.logInInBackgroundWithAccessToken(accessToken, block: {
            (user: PFUser?, error: NSError?) -> Void in
            if user != nil {
                print("User logged in through Facebook!")

                self.navigateToInGame(true)

            } else {
                print("Uh oh. There was an error logging in.")

                let manager = FBSDKLoginManager()
                manager.logOut()
                manager.logInWithReadPermissions(["public_profile", "email"], fromViewController: self, handler: { (result, error) -> Void in
                    if let error = error {
                        print(error.localizedDescription)
                    }

                    if result.isCancelled {
                        print("Cancelled")
                    } else {
                        print("Logged in")
                        if let user = user {
                            if user.isNew {
                                print("User signed up and logged in through Facebook!")

                                self.performSegueWithIdentifier("signUp", sender: self)

                            } else {
                                print("User logged in through Facebook!")

                                self.navigateToInGame(true)
                            }
                        } else {
                            print("Uh oh. The user cancelled the Facebook login.")
                        }

                    }
                })

                if accessToken.expirationDate.compare(NSDate()) == NSComparisonResult.OrderedAscending {
                    FBSDKLoginManager.renewSystemCredentials({ (result, error) -> Void in
                        if result != .Renewed {
                            // Do further procedures once renewed is failed or rejected
                            print("Token didn't renew")
                        }
                    })
                }

            }
        })
    } else {
        let permissions = ["public_profile", "email"]
        PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions, block: {
            (user: PFUser?, error: NSError?) -> Void in
            if let user = user {
                if user.isNew {
                    print("User signed up and logged in through Facebook!")

                    self.performSegueWithIdentifier("signUp", sender: self)

                } else {
                    print("User logged in through Facebook!")

                    self.navigateToInGame(true)
                }
            } else {
                print("Uh oh. The user cancelled the Facebook login.")
            }
        })
    }
}

UPDATED CODE.

I get the following in log:

"uh oh, error logging in". Still allows me to login via FB. I click "Continue" in Facebook.

Log reads "logged in"

Log reads "Uh oh, user cancelled the Facebook login and it kicks me back to the login screen. The user, however, is still created in Parse. I would like to go to my signUp segue after signing in for the first time.

winston
  • 3,000
  • 11
  • 44
  • 75

1 Answers1

1
if result.isCancelled {
    print("Cancelled")
} else {
    result.token // Your new token is here and throw the token into logInInBackgroundWithAccessToken(:) again
}
//else {
//    print("Logged in")
//    if let user = user {
//        if user.isNew {
//            print("User signed up and logged in through Facebook!")
//            
//            self.performSegueWithIdentifier("signUp", sender: self)
//            
//        } else {
//            print("User logged in through Facebook!")
//            
//            self.navigateToInGame(true)
//        }
//    } else {
//        print("Uh oh. The user cancelled the Facebook login.")
//    }
//    
//}

The part if let user = user { ... } doesn't make sense to me because a new token result.token just got by func logInWithReadPermissions. Therefore, the updated token should be put through PFFacebookUtils.logInInBackgroundWithAccessToken(:) again.

Allen
  • 1,714
  • 17
  • 19
  • Thanks for the reply! I put that code right where the error is logging but it's not getting passed the if statement. Do you know how to convert this answer to swift? http://stackoverflow.com/questions/29421659/reset-facebook-token-reference-facebook-sdk-4-0 . I think that may be the answer. I need to log out the user and reset the token – winston Mar 11 '16 at 11:21
  • @james I just updated my answer. Please take it for a reference. Cheers! – Allen Mar 11 '16 at 14:13
  • Thank you so much for this. I'm hoping this works because the user didn't truly "log out" (I removed the user from the User table for testing, then removed the app in FB). I'll let you know if this works! – winston Mar 11 '16 at 14:48
  • That seemed to get me closer! Updated my question and code. It seems to renew the token (lets me login again with my FB account) but then it brings me back to my login screen – winston Mar 11 '16 at 23:17
  • @james after login and got a new token. user is still nil before going through ``PFFacebookUtils.logInInBackgroundWithAccessToken``. Please check updated answer. – Allen Mar 12 '16 at 00:42
  • Thank you so much for your help. I think this resolved the issue! My segue isn't working but I do understand that that is a separate issue. I made a separate question here http://stackoverflow.com/questions/35953021/swift-segue-not-working Thanks again!! – winston Mar 12 '16 at 02:25