1

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

当用户使用“正常”注册功能(没有脸书)时,一切都像黄油一样平稳。然而,现在我向Facebook注册表中添加了相同的信息,它给出了这个致命错误,当swift发现意外的时候发生了意外的错误,同时展开一个可选值。

我知道它必须是我添加到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) 

    } 


} 

回答

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

    } 

您需要包裹签到图请求里面,所以你可以拉从标志UID,以及获得来自graphrequest的信息。这也将允许您使用其他登录方法,例如Google,并仍然提取正确的uid。

相关问题