Firebase不会使用Facebook App用户ID创建Facebook帐户用户UID

时间:2016-05-21 20:52:46

标签: swift facebook firebase firebase-authentication fbsdk

我正在开发一个swift的iOS应用程序,要求用户通过连接他们的Facebook帐户来创建帐户。一个月前,当我通过注册我自己的Facebook帐户进行测试时,Firebase创建的用户UID格式为" facebook:(facebook app id)"。这就是我想要的。

但是,最近,只要用户通过将Facebook帐户与我的应用程序相关联来创建新的acconut,Firebase就会使用随机字符串创建用户UID。例如:" E9FaL87wRmOKfhen2S6yszhCwtx1"。这可能是因为新的Firebase更新?我应该完成迁移过程吗?

以下是我创建帐户的代码:

@IBAction func facebookAuthAction(sender: AnyObject) {
    let facebookLogin = FBSDKLoginManager()

    facebookLogin.logInWithReadPermissions(nil, fromViewController: nil, handler: {(facebookResult, facebookError) -> Void in

        if facebookError != nil {
            print("Facebook login failed. Error \(facebookError)")
        } else if facebookResult.isCancelled {
            print("Facebook login was cancelled.")
        } else {
            let accessToken = FBSDKAccessToken.currentAccessToken().tokenString

            FIREBASE_REF.authWithOAuthProvider("facebook", token: accessToken, withCompletionBlock: { error, authData in

                    if error != nil {
                        print("Login failed. \(error)")
                    } else {
                        print("Logged in! \(authData)")
                        NSUserDefaults.standardUserDefaults().setValue(authData.uid, forKey: "uid")

                        FIREBASE_REF.childByAppendingPath("users").observeEventType(.Value, withBlock: {snapshot in
                            if snapshot.hasChild(authData.uid) == false {
                                self.createNewFBUser(authData.uid)
                            }
                        })

                        self.performSegueWithIdentifier("loginSegue", sender: self)
                    }
            })
        }
    })
}

func createNewFBUser(uid: String) {
    var emailAddress:String = ""
    var firstName:String = ""
    var lastName:String = ""
    var pictureURL:String = ""

    let parameters = ["fields": "email, first_name, last_name, picture.type(large)"]
    FBSDKGraphRequest(graphPath: "me", parameters: parameters).startWithCompletionHandler {(connection, result, error) -> Void in

        if error != nil {
            print(error)
            return
        }

        if let email = result["email"] as? String {
            emailAddress = email
        }
        if let first_name = result["first_name"] as? String {
            firstName = first_name
        }
        if let last_name = result["last_name"] as? String {
            lastName = last_name
        }
        if let picture = result["picture"] as? NSDictionary, data = picture["data"] as? NSDictionary, url = data["url"] as? String {
            pictureURL = url
        }

        let newUser = [
            "provider": "facebook",
            "firstName": firstName,
            "lastName": lastName,
            "email": emailAddress,
            "picture": pictureURL
        ]
        FIREBASE_REF.childByAppendingPath("users").childByAppendingPath(uid).setValue(newUser)
    }
}

1 个答案:

答案 0 :(得分:1)

将项目升级到new Firebase Console时,您的用户将迁移到新的身份验证后端。

升级后新创建的用户将获得新格式的uid。有关此更改的详细信息以及何时将其应用于现有(未升级的)Firebase应用,请参阅此post on the firebase-talk group

请注意,Firebase建议多年来取决于uid的格式。最好将其视为标识用户的不透明字符串。