Firebase 不使用 Facebook 应用用户 ID 创建 Facebook 帐户用户 UID

Firebase does not create a Facebook account User UID using the Facebook App User ID

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

但是,最近每当用户通过将他们的 Facebook 帐户与我的应用程序连接来创建新帐户时,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)
    }
}

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

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

请注意,Firebase 多年来一直建议不要根据 uid 的格式。最好将其视为标识用户的不透明字符串。