Can you share the code for the ViewController in which you are trying to create a “user” entry. The code snippet you have provided means that more questions are going to be asked for you to show what you have done so far in your code.
For any of us to help, it is useful to have the full context of your code.
can you try to put your database/firebase in “test mode” (allow read and write for everyone) so we know its not an authentication/database restriction issue?
@IBOutlet weak var EmailTextField: UITextField!
@IBOutlet weak var PasswordTextField: UITextField!
@IBOutlet weak var LoginButton: UIButton!
@IBOutlet weak var ErrorLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setUpElements()
}
func setUpElements() {
//Hide error label
ErrorLabel.alpha = 0
//Style the elememts
Utilities.styleTextField(EmailTextField)
Utilities.styleTextField(PasswordTextField)
Utilities.styleFilliedButton(LoginButton)
}
//Check the fields and validate that the data is correct. If everything is correct, this method returns nil. Otherwise it returns the error message
func validateFields() -> String? {
//Check that all fields are filled in
if EmailTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" || PasswordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == ""{
return "Please fill in all fields"
}
return nil
}
@IBAction func LoginButtonTapped(_ sender: Any) {
// Create cleaned versions of the text field
let email = EmailTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let password = PasswordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
//Signing in the user
Auth.auth().signIn(withEmail: email, password: password) { (result, error) in
if error != nil {
//Couldn't sign in
self.ErrorLabel.text = error!.localizedDescription
self.ErrorLabel.alpha = 1
}
else {
let homeViewController =
self.storyboard?
.instantiateViewController(identifier:
Constants.Storyboard.homeViewController) as?
HomeViewController
self.view.window?.rootViewController = homeViewController
self.view.window?.makeKeyAndVisible()
}
}
}
}
*** let db = Firestore.firestore()
db.collection("users").addDocument(data: ["firstname": firstname, "lastname": lastname, "uid": result!.user.uid]) { (error) in
if error != nil{
// Show error message
self.shownError("Error saving user data")
}
}