gpt4 book ai didi

ios - 为什么 Auth.auth().currentUser?.uid 返回一个 nil 值?

转载 作者:搜寻专家 更新时间:2023-11-01 06:55:58 26 4
gpt4 key购买 nike

我想将用户存储在他们的 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: fatal error :在我尝试打印 firebaseID 的行上展开可选值时意外发现 nil。为什么Auth.auth().currentUser?.uid 的值为nil,如何获取该值?

最佳答案

你同时发起了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)
}

}


}

}

关于ios - 为什么 Auth.auth().currentUser?.uid 返回一个 nil 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53347132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com