Trying to manage the signed in account, by assigning it to metadata (a username and image). I've successfully signed in the account with facebook, now i just need firebase to accept the metadata associated with that user, so that I have a reference to make social media posts with.
The problem is I have no idea what I'm doing, I've followed a few tutorials on how to login with firebase/facebook and I'm trying to combine the code from each tutorial.
Once signed in, I run a function called getUserData(), which I believe is supposed to generate an array of user image URL's that can be referenced in firebase database (to retrieve later).
The app crashes on this line: self.currentUserImageUrl = postDict["userImg"] as! String
The error I get is: "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value."
I really wish someone could teach me what this problem means so that i can move forward.
This logs the facebook user in:
@objc func handleCustomFBLogin(){
FBSDKLoginManager().logIn(withReadPermissions: ["email", "public_profile"], from: self){
(result, err) in
if err != nil {
print("Custom FB login failed:", err)
return
}
self.showEmailAdress()
}
}
I think this provides a firebase reference for the facebook user:
func showEmailAdress() {
let accessToken = FBSDKAccessToken.current()
guard let accessTokenString = accessToken?.tokenString else
{return}
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
Auth.auth().signIn(with: credential, completion: { (user,
error ) in
if error != nil {
print("Something went wrong with the FB user:", error)
return
}
self.setupUser(userUid: (user?.uid)!)
KeychainWrapper.standard.set((user?.uid)!, forKey: "uid")
self.performSegue(withIdentifier: "toFeed", sender: nil)
print("Successfully logged in with user:", error)
})
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start { (connection, result, err) in
if err != nil{
print("Failed to start graph request", err ?? "")
return
}
print(result ?? "")
}
}
I think this handles the metadata for the user:
func setupUser(userUid: String) {
if let imageData = UIImageJPEGRepresentation(self.userImgView.image!, 0.2) {
let imgUid = NSUUID().uuidString
let metaData = StorageMetadata()
Storage.storage().reference().child(imgUid).putData(imageData, metadata: metaData) { (metadata, error) in
let downloadURL = metadata?.downloadURL()?.absoluteString
let userData = [
"username": self.usernameField.text!,
"userImg": downloadURL
] as [String : Any]
Database.database().reference().child("users").child(userUid).setValue(userData)
self.performSegue(withIdentifier: "toFeed", sender: nil)
}
}
}
App crashes on this function:
func getUsersData(){
let uid = KeychainWrapper.standard.string(forKey: "uid")
Database.database().reference().child("users").observeSingleEvent(of: .value) { (snapshot) in
if let postDict = snapshot.value as? [String : AnyObject] {
self.currentUserImageUrl = postDict["userImg"] as! String
self.tableView.reloadData()
}
}
}