Facebook Login does not create user in Database - Firebase V3 - iOS/Swift

686 views
Skip to first unread message

Vincent Rolea

unread,
May 21, 2016, 11:26:49 AM5/21/16
to Firebase Google Group
I started to play around with the new Firebase SDK, and I just wanted to create a login flow with facebook. I followed the new tutorial, I am able to log my users with facebook, however, this does not create a new user in the realtime database. 
My code is the following: 

class ViewController: UIViewController, FBSDKLoginButtonDelegate {


    override func viewDidLoad() {

       super.viewDidLoad()

       

       let loginButton = FBSDKLoginButton()

       loginButton.center = self.view.center

       self.view.addSubview(loginButton)

       loginButton.delegate = self


    }




    override func didReceiveMemoryWarning() {


        super.didReceiveMemoryWarning()


        // Dispose of any resources that can be recreated.


    }


   


    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {


        if let error = error {


            print(error.localizedDescription)


            return


        } else {


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


            FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in


                if let error = error {


                    print(error.localizedDescription)


                } else {


                    print("successfullyAuthenticated")


                }


            }


        }


    }


   


    func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {


        print("logged out")


    }


}


With the previous version of Firebase, I used a Facebook Manager object and not the Facebook Login button, which allowed me to add more config, I don't know if it's still possible with the new version

Mike Mcdonald

unread,
May 21, 2016, 6:18:23 PM5/21/16
to Firebase Google Group
Hi Vincent,

This is a super common use case, so sorry that we don't have a better example of this in the docs. That said, you're super lucky, since I just built an example of this not too long ago:

// Log in to Facebook


      let login = FBSDKLoginManager()


      login.logInWithReadPermissions(["public_profile"], fromViewController: self, handler: { (result, error) in


        if (error != nil || result.isCancelled) {


          print(error)


        } else {


          // Log in to Firebase via Facebook


          let credential = FIRFacebookAuthProvider.credentialWithAccessToken(result.token.tokenString)


          FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in


            if (error != nil) {


              print(error)


            }


          }


        }


      })


// Observe auth state change


    self.auth.addAuthStateDidChangeListener { (auth, user) in


      if (user != nil) {


        let uid = user?.uid as String!


        self.database.reference().child("users/\(uid)").setValue(["name": user?.displayName]) // as well as other info


      }


    }


We don't automatically sync user data to the database, but as you can see it's very simple to wire up :)

Thanks,
--Mike

Vincent Rolea

unread,
May 25, 2016, 11:20:49 AM5/25/16
to Firebase Google Group
Hi Mike!,

Very simple Indeed! Actually I used the exact same method with the previous SDK, I simply didn't read correctly the new tutorials and thought it was created records in the RT DataBase! Thanks for the clear answer ! 

Vincent
Reply all
Reply to author
Forward
0 new messages