iosfan iosfan - 2 months ago 11
Swift Question

Firebase how to create users?

I am not familiar with the new Firebase. How do I create new users? The code below I Signup and

auth
new user. If I need to create this new user under "Customers" in
Firebase Database
, what code do I need to add? Thanks!

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

if err != nil {

self.showAlert("Can't Register", msg: "Please enter email and password")

} else {

NSUserDefaults.standardUserDefaults().setValue(user?.uid, forKey: "uid")

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

})

self.performSegueWithIdentifier("toSecondVC", sender: self)
}

})

Answer
FIRAuth.auth()?.createUserWithEmail(email, password: password, completion: { (user, err) in

 if err != nil {

     self.showAlert("Can't Register", msg: "Please enter email and password")

  } else {   

      NSUserDefaults.standardUserDefaults().setValue(user?.uid, forKey: "uid")

     FIRDatabase.database().reference().child("Customers").setValue([user!.uid : "true"])

     //Or if you want to save the users Details too:- 
     /*

        FIRDatabase.database().reference().child("Customers").setValue([user!.uid : ["email" : email,"password" : password]])

     */
     self.performSegueWithIdentifier("toSecondVC", sender: self)
    }
})

Also might i suggest reading this : Firebase iOS - Save Data