【问题标题】:How to store the uid of the Facebook users in Firebase?如何在 Firebase 中存储 Facebook 用户的 uid?
【发布时间】:2017-08-28 21:14:34
【问题描述】:

我刚刚将 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)

    }


}

【问题讨论】:

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


    【解决方案1】:

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

    //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,还可以访问来自图形请求的信息。这也将允许您使用其他登录方法,例如 Google,并且仍然提取正确的 uid。

    【讨论】:

      猜你喜欢
      • 2017-10-11
      • 2017-12-11
      • 1970-01-01
      • 2018-06-23
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      相关资源
      最近更新 更多