Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Simen Anthonsen
Simen Anthonsen
1,097 Points

Register users in parse database when user log in with Facebook

Ok, in my app I have successfully integrated my app with Parse and I have created Log In and Sign Up options, which works. When a user signs up, it registers on my Parse account. I have also created a Facebook login options which also works, but the user dont registers on my Parse account. I will give you my code on the Facebook login options below

My viedDidLoad consists of this

                                //To check wether the user is already logged in or not        
                                if (FBSDKAccessToken.currentAccessToken() == nil)
    {
        print("Not logged in..")
    }
    else
    {
        print("Logged in..")
    }
    //Creation of the Facebook button
    var loginButton = FBSDKLoginButton()
    loginButton.readPermissions = ["public_profile", "email", "user_friends"]
    loginButton.center = self.view.center
    loginButton.delegate = self
    self.view.addSubview(loginButton)

Then I have got these three functions in my viewControllerClass.

                         func loginWithFB() {
    PFFacebookUtils.logInInBackgroundWithReadPermissions(["public_profile", "email", "user_friends"]) {
        (user: PFUser?, error: NSError?) -> Void in
        if let user = user {
            if user.isNew {
                print("User signed up and logged in through Facebook!")
            } else {
                print("User logged in through Facebook!")
            }
        } else {
            print("Uh oh. The user cancelled the Facebook login.")
        }
    }

}

                         func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
{
    if error == nil
    {
        print("Login complete.")
        self.performSegueWithIdentifier("showNew", sender: self)
    }
    else
    {
        print(error.localizedDescription)
    }
}

func loginButtonDidLogOut(loginButton: FBSDKLoginButton!)
{
    print("User logged out...")
}

This works great, but how do I integrate these function with Parse, making sure the user is being registered in my Parse account?