0

I am trying to create a user in Firebase and save the user information at the time the user gets created. This is my action function where I create the user:

@IBAction func createAccountButton(_ sender: AnyObject) {

            FIRAuth.auth()?.createUser(withEmail: emailTextField.text!, password: passwordTextField.text!, completion: { (user, error) in
                if error != nil {
                    print(error!.localizedDescription)
                } else {
                    let userID =  (user?.uid)! as String
                    self.bringerUser.userRef = userID as String
                    let userEmail:String = self.emailTextField.text!
                    print(userEmail)
                    print(self.bringerUser.userRef! as String)
                    self.ref.child("users").child(self.bringerUser.userRef! as String).child("userInfo").setValue(["email": emailTextfield.text])
                }
            })
    }

The user ID is nil when trying to access and write data on Firebase but if I print it it works fine. The error I get is this:

fatal error: unexpectedly found nil while unwrapping an Optional value

cosmos
  • 153
  • 1
  • 15

1 Answers1

3

Try :-

FIRDatabase.database().reference().child("users").child(FIRAuth.auth()!.currentUser!.uid).child("userInfo").setValue(["email": emailTextfield.text])
Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • I get an error : Value of type 'FIRAuth?' has no member 'auth' and also: 'init()' is unavailable. – cosmos Nov 02 '16 at 22:00
  • Getting this error: fatal error: unexpectedly found nil while unwrapping an Optional value. The userID prints the user ID with no errors. – cosmos Nov 02 '16 at 22:04
  • Ok. I think you are right. I cant test it since I am getting this error: We have blocked all requests from this device due to unusual activity. Try again later. I dont know what it means but it seems Firebase is not letting me create accounts. – cosmos Nov 02 '16 at 22:12