I want to store users under their Firebase generated uid. When I attempt to use this code:
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!)
}
}
The error I am given is: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value on the line where I am attempting to print the firebaseID. Why is the value of Auth.auth().currentUser?.uid nil and what can I do to obtain this value?