0

I'm setting up an app that uses firebase. What I'm looking to do is just get the associated values of one user off of the site on login. I have the seed function set up properly, but I want to pull the user object I have created here:

 // MARK: - Firebase Register
func firebaseRegister() {

    var allFieldsAreFilled = true

    for textField in requiredTextFields {
        if textField.text == "" {

            allFieldsAreFilled = false

        }
    }

    if allFieldsAreFilled {

        if let email = emailTextField.text, password = passwordTextField.text, givenName = firstNameTextField.text, familyName = lastNameTextField.text, state = stateTextField.text, phoneNumber = phoneNumberTextField.text {

            if password == verifyPasswordTextField.text {

                if email == verifyEmailTextField.text {

                    FIRAuth.auth()?.createUserWithEmail(email, password: password) { (user, error) in

                        if error != nil {

                            print(error)

                        } else {

                            self.ref.child("users").child(user!.uid).setValue(

                                ["email": email,
                                    "password": password,
                                    "givenName":givenName,
                                    "familyName": familyName,
                                    "state": state,
                                    "phoneNumber": phoneNumber]

                            )
                        }
                    }
                }
            }

        } else {

            presentAlert("Please fill all fields")

        }
    }
}

I want to pull a child from the users path by email. I am having trouble using the query examples to do so. Thanks in advance!

Sean Calkins
  • 312
  • 2
  • 11
  • Possible duplicate of [How I can read an item through keys in firebase?](http://stackoverflow.com/questions/38241129/how-i-can-read-an-item-through-keys-in-firebase) – adolfosrs Jul 11 '16 at 16:25
  • 3
    Nice [pyramid of doom](https://en.wikipedia.org/wiki/Pyramid_of_doom_(programming))... ;) You should use Swift's ability to merge several `if let` together instead. – Eric Aya Jul 11 '16 at 16:27
  • What does *pull one user off of the site mean*? To remove the user? You question title says you want to query one specific object but you code does not contain any query. If you can clarify your question, we'll probably be able to answer. – Jay Jul 11 '16 at 17:46
  • Oh, and don't forget that if you create a user, you unauthenticate yourself and authenticate as that user: see [Firebase User Kicker 'feature'](http://stackoverflow.com/questions/37517208/firebase-kicks-out-current-user) – Jay Jul 11 '16 at 17:49
  • Sorry, I have updated the question. I want to get all of the key value pairs associated with one user I have created, by inputting the email into the query parameters. The provided code is to clarify that I am not trying to get the user firebase creates for you using their authentication method. I want to get the values from the child I created. – Sean Calkins Jul 11 '16 at 18:12
  • You want to query for the user created with the code in your question... What are the query parameters? Do you know the uid? Or by email or phone number? – Jay Jul 11 '16 at 23:45
  • By email was the goal, but I figured it out – Sean Calkins Jul 12 '16 at 00:02
  • @EricD or better using `guard` – iOSGeek Jul 12 '16 at 15:10

1 Answers1

1

Here is the block of code that I am currently using, it also saves the user into my core data if it doesn't exist

self.ref.child("users").queryOrderedByChild("email").queryEqualToValue("\(email)").observeEventType(.Value, withBlock: { snapshot in

                    if ( snapshot.value is NSNull ) {

                        print("username does not exist")

                    } else {

                        if !DataController.sharedInstance.usernameExists(email) {

                            if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {

                                for snap in snapshots {

                                    if let dict = snap.value as? Dictionary<String, AnyObject> {

                                        let key = snap.key

                                        if let givenName = dict["givenName"] as? String, familyName = dict["familyName"] as? String, email = dict["email"] as? String, password = dict["password"] as? String, state = dict["state"] as? String, phoneNumber = dict["phoneNumber"] as? String {

                                            DataController.sharedInstance.seedUser(key, email: email, given_name: givenName, family_name: familyName, password: password, state: state, phoneNumber: phoneNumber)

                                        }
                                    }
                                }
                            }
                        }

                        self.currentUser = DataController.sharedInstance.fetchUserByEmail(email)

                    }
                })
Sean Calkins
  • 312
  • 2
  • 11