【问题标题】:Why did Auth.auth().currentUser?.uid return a nil value?为什么 Auth.auth().currentUser?.uid 返回 nil 值?
【发布时间】:2025-12-05 14:40:01
【问题描述】:

我想将用户存储在他们的 Firebase 生成的 uid 下。当我尝试使用此代码时:

  func showCredentials() {
        let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
        Auth.auth().signInAndRetrieveData(with: credential) { (AuthDataResult, error) in
            if error != nil {
                print("Something went wrong with our Facebook User: ", error)
                return
            }
            print("Successfuly logged in with our Facebook User: ", AuthDataResult)
            self.performSegue(withIdentifier: "FacebookSegue", sender: self)
        }
        FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start {
            (connection, graphResult, error) in
            if error != nil {
                print("Failed to Start Graph Request:", error)
                return
            }
            print(graphResult)
            let facebookDetail = graphResult as! NSDictionary
            let userID = facebookDetail["id"]
            let fullName = facebookDetail["name"]
            let email = facebookDetail["email"]
            let providerName = "Facebook"
            let userValues = (["Email": email, "Full Name": fullName, "UID": userID, "Provider":providerName])
            let databaseRef = Database.database().reference()
            let key = databaseRef.child("node").childByAutoId().key ?? ""
            let childUpdates = ["/Users/\(userID))/": userValues]
            databaseRef.updateChildValues(childUpdates)
            print(userID!)
            print(fullName!)
            let firebaseID = Auth.auth().currentUser?.uid
            print(firebaseID!)
        }
    }

我得到的错误是:线程 1:致命错误:在我尝试打印 firebaseID 的行上展开可选值时意外发现 nil。为什么 Auth.auth().currentUser?.uid 的值为 nil,如何获取该值?

【问题讨论】:

  • 我之前在尝试检索用户的 uid 时遇到了这个问题。您的不能保证返回当前用户的信息,这就是为什么 uid 并且可能当前用户信息为零的原因。您需要等待响应返回,然后才能检索用户的信息。您也许可以初始化一个全局变量 uid,然后在值返回后为其分配一个值。也许这可以帮助github.com/ThriveCommunityChurch/ThriveChurchOfficialApp/issues/…

标签: ios swift xcode facebook firebase


【解决方案1】:

你同时初始化2个异步请求

Auth.auth().signInAndRetrieveDataFBSDKGraphRequest(graphPath: "/me"

由于身份验证尚未完成,第二个可能首先完成导致崩溃,因此您需要嵌套它们以确保您在身份验证登录完成后访问用户

func showCredentials() {
    let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
    Auth.auth().signInAndRetrieveData(with: credential) { (AuthDataResult, error) in
        if error != nil {
            print("Something went wrong with our Facebook User: ", error)
            return
        }
        FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start {
        (connection, graphResult, error) in
        if error != nil {
            print("Failed to Start Graph Request:", error)
            return
        }
        print(graphResult)
        let facebookDetail = graphResult as! NSDictionary
        let userID = facebookDetail["id"]
        let fullName = facebookDetail["name"]
        let email = facebookDetail["email"]
        let providerName = "Facebook"
        let userValues = (["Email": email, "Full Name": fullName, "UID": userID, "Provider":providerName])
        let databaseRef = Database.database().reference()
        let key = databaseRef.child("node").childByAutoId().key ?? ""
        let childUpdates = ["/Users/\(userID))/": userValues]
        databaseRef.updateChildValues(childUpdates)
        print(userID!)
        print(fullName!)
        let firebaseID = Auth.auth().currentUser?.uid
        print(firebaseID!)

        DispatchQueue.main.async {
         print("Successfuly logged in with our Facebook User: ", AuthDataResult)
         self.performSegue(withIdentifier: "FacebookSegue", sender: self)
        }

    }


    }

}

【讨论】:

  • 我知道我想获取 Auth.auth().currentUser?.uid 令牌作为字符串并将用户存储在相应的令牌下。
  • @JustinReddick 你应该将此标记为正确答案
最近更新 更多