如何在Firebase中存储Facebook用户的uid? [英] How to store the uid of the Facebook users in Firebase?

查看:96
本文介绍了如何在Firebase中存储Facebook用户的uid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

当用户使用普通"注册功能(不使用Facebook)时,一切都像黄油一样顺利.但是,现在我将相同的信息添加到Facebook注册表中,它给出了致命错误,当swift在展开可选值时意外地发现nil时,会发生该致命错误.

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.

我知道它必须是我要添加到FIRDatabase中的uid.但是我还没有弄清楚如何在其他地方获取uid.

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)

    }


}

推荐答案

这就是我完成您尝试做的事情的方式.

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

    }

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

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.

这篇关于如何在Firebase中存储Facebook用户的uid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆