Home Swift Firebase Facebook Login - Gets Name, but email returns nil
Reply: 0

Swift Firebase Facebook Login - Gets Name, but email returns nil

user5122
1#
user5122 Published in August 15, 2018, 8:43 pm

Recently my facebook login hasn't been working. It doesn't save into my database but in Firebase authentication it shows someone signed in with facebook but the email is blank. It doesn't save any data to my database which it should. The email is returning nil. My google sign in works. I've checked all the connections and they all seem to be fine. (I could have missed something) anyone have any suggestions on what connections I should check? Not sure what to do...

More Info

I printed my graph request... not sure if this helps to debug

let loginManager: FBSDKLoginManager = FBSDKLoginManager()
        loginManager.logIn(withReadPermissions: self.facebookPermissions, from: self, handler: { (result, error) in
            if error != nil {
                loginManager.logOut()
                let message: String = "An error has occured. \(String(describing: error))"
                let alertView = UIAlertController(title: "Alert", message: message, preferredStyle: UIAlertControllerStyle.alert)
                alertView.addAction(UIAlertAction(title: "Ok ", style: UIAlertActionStyle.default, handler: nil))
                self.present(alertView, animated: true, completion: nil)
            } else if (result?.isCancelled)! {
                // user cancelled login
                loginManager.logOut()
            } else {
                let accessToken = FBSDKAccessToken.current()
                guard let accessTokenString = accessToken?.tokenString else { return }
                let credential = FacebookAuthProvider.credential(withAccessToken: accessTokenString)

                Auth.auth().signIn(with: credential) { (user, error) in
                    if (error != nil) {
                        // handle error
                        print(error ?? "Error")
                    } else {
                        let ref = Database.database().reference()

                        // guard for user id
                        guard let uid = user?.uid else {
                            return
                        }
                        let usersReference = ref.child("user_profiles").child(uid)

                        let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, email"])
                        graphRequest.start(completionHandler: { (connection, result, error) -> Void in
                            if error != nil {
                                // Process error
                                print("Error: \(String(describing: error))")
                            } else {
                                guard let data: [String:AnyObject] = result  as? [String:AnyObject] else {
                                    print("Can't pull data from JSON")
                                    return
                                }
                                guard let userName: String = data["name"] as? String else {
                                    print("Can't pull username from JSON")
                                    return
                                }
                                guard let userID: String = data["id"] as? String else {
                                    print("Can't pull ID from JSON")
                                    return
                                }
                                let imgURLString = "http://graph.facebook.com/\(userID)/picture?type=large" as String
                                guard let userEmail: String = data["email"] as? String else {
                                    print("Can't pull email from JSON")
                                    print("Error: \(String(describing: error))")
                                    return
                                }


                                // initial # posts = 0
                                let values = ["name": userName, "email": userEmail, "facebookID": userID, "profPicString": imgURLString] as [String : Any]

                                // update database with new user
                                usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
                                    // error in database save
                                    if err != nil {
                                        print(err ?? "Error saving user to database")
                                        return
                                    }
                                })
                            }
                        })
                        self.dismiss(animated: false, completion: nil)
                        // Present the main view
                        if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "Customer Profile") {
                            UIApplication.shared.keyWindow?.rootViewController = viewController
                            self.dismiss(animated: true, completion: nil)
                        }
                    }
                }
            }
        })

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.295359 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO