cosmos cosmos - 1 year ago 86
Swift Question

Set Values when user is created on Firebase - Swift

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 {
} else {
let userID = (user?.uid)! as String
self.bringerUser.userRef = userID as String
let userEmail:String = self.emailTextField.text!
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

Answer Source

Try :-

FIRDatabase.database().reference().child("users").child(FIRAuth.auth()!.currentUser!.uid).child("userInfo").setValue(["email": emailTextfield.text])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download