How to store the uid of the Facebook users in Firebase?

孤街醉人 提交于 2019-12-08 04:41:38

问题


I have just integrated the Facebook Login function to my Firebase App. I want to add some extra information to the user in the FIRDatabase. Thats why I add a child user with the uid as the identifier to my firebase database.

When a user uses the "normal" sign up function (without Facebook) everything works smooth like butter. However now that I added the same information to the Facebook register form, it gives that fatal error which occurs when swift found unexpectedly nil while unwrapping an optional value.

I am aware that it has to be the uid I am adding to the FIRDatabase. But I haven't figured out yet how to get the uid elsewhere.

var FirebaseUId: String!
func showEmailAddress() {
    let accessToken = FBSDKAccessToken.current()
    guard let accessTokenString = accessToken?.tokenString else {return}

    let credentials = FIRFacebookAuthProvider.credential(withAccessToken: accessTokenString)

    FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in
        if error != nil {
            print("Something went wrong with our FB user:", error ?? "")
            return
        }
        print("succesfullex logged in with our user: ", user ?? "")
         self.FirebaseUId = user?.uid
    })
    FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start { (connection, result, err) in
        if err != nil {

            return

        }
        print(result ?? "")
        let data = result as! [String : AnyObject]
        let email = data["email"] as? String
        let username = data["name"] as? String

        let userInfo: [String : Any] = ["uid" : self.FirebaseUId!  ,
                                        "username" : username!,
                                        "Email" : email!,
                                        "Passwort" : " ",
                                        "Geschlecht" : " ",
                                        "urltoImage" : " ",
                                        "FußballPreferenz" : " ",
                                        "BasketballPreferenz" : " ",
                                        "FußballBesitz" : " ",
                                        "BasketballBesitz" : " ",
                                        "LeibchenBesitz" : " "]
        var ref: FIRDatabaseReference!
        ref = FIRDatabase.database().reference()

        ref.child("users").child(self.FirebaseUId).setValue(userInfo)

    }


}

回答1:


This is how I accomplished what you are trying to do.

//this is what we need to save to FB
    FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start {  //this gets info
        (connection, result, err) in

        if err != nil {
            print("graph failed", err as Any)
            return
        }

    Auth.auth().signIn(with: credential, completion: { (user, error) in    //this logs you in
        if error != nil {
            print("something wrong", error as Any)
            return
        }
        let uid = user?.uid

        let ref = Database.database().reference()              //below adds the info to the db
        let usersRef = ref.child("users").child(uid!)

        usersRef.updateChildValues(result as! [AnyHashable : Any])

        print("userID", uid as Any)
        })

    }

You will need to wrap the signIn inside the graph request so you can pull the uid from the sign in, and also access the info from the graphrequest. This will also allow you to use other sign in methods as well, such as Google, and still pull the correct uid.



来源:https://stackoverflow.com/questions/43194209/how-to-store-the-uid-of-the-facebook-users-in-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!