如何在Firebase中存储Facebook用户的uid?

时间:2017-04-03 21:02:58

标签: ios swift3 firebase-realtime-database firebase-authentication facebook-login

我刚刚将Facebook登录功能集成到我的Firebase应用程序中。我想在FIRDatabase中向用户添加一些额外的信息。这就是为什么我添加一个带有uid的子用户作为我的firebase数据库的标识符。

当用户使用“正常”注册功能(没有Facebook)时,一切都像黄油一样顺畅。然而,现在我将相同的信息添加到Facebook注册表单中,它会给出致命错误,当swift在打开可选值时意外发现nil时会发生这种错误。

我知道它必须是我添加到FIRDatabase的uid。但我还没弄明白如何将uid放到其他地方。

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 个答案:

答案 0 :(得分:0)

这就是我完成你想要做的事情。

//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)
        })

    }

您需要在图形请求中包含signIn,以便您可以从登录中提取uid,还可以从graphrequest访问信息。这也允许您使用其他登录方法,例如Google,并且仍然可以使用正确的uid。