Cloud Firestore Authentication iOS











up vote
0
down vote

favorite












I'm very new to iOS development (learning as I go) and I'm using Cloud Firestore to authenticate users via email/password. I'm trying to do a simple email & password authentication. I used one of the db rules from documentation and I don't think I have to create collections and documents in the rules. I've already tested my connection to the db so that's not an issue either. The code I have so far is below:



import UIKit
import Firebase


class SignUpViewController: UIViewController {

//Outlets
@IBOutlet weak var firstNameText: UITextField!
@IBOutlet weak var lastNameText: UITextField!
@IBOutlet weak var emailText: UITextField!
@IBOutlet weak var passwordText: UITextField!
@IBOutlet weak var signUpButton: UIButton!


override func viewDidLoad() {
super.viewDidLoad()
}

@IBAction func signUpButtonTapped(_ sender: Any) {
guard let firstName = firstNameText.text,
let lastName = lastNameText.text,
let email = emailText.text,
let password = passwordText.text else { return }

Auth.auth().createUser(withEmail: email, password: password) { (authResult, error) in
if let error = error {
debugPrint("Error creating user: (error.localizedDescription)")
}
let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
changeRequest?.displayName = firstName
changeRequest?.commitChanges(completion: { (error) in
if let error = error {
debugPrint(error.localizedDescription)
}
})
guard let userId = authResult?.user else { return }
Firestore.firestore().collection(USERS_REF).document(userId).setData([
USERNAME : firstName,
DATE_CREATED : FieldValue.serverTimestamp()
], completion: { (error) in

if let error = error {
debugPrint(error.localizedDescription)
} else {
self.dismiss(animated: true, completion: nil)
}
})
}
}



}


But I get the error:




  • Cannot invoke 'setData' with an argument list of type '([String : Any], completion: ((Error?) -> Void)?)'


Is there something I'm not doing? Could someone help with this email/password authentication?










share|improve this question




























    up vote
    0
    down vote

    favorite












    I'm very new to iOS development (learning as I go) and I'm using Cloud Firestore to authenticate users via email/password. I'm trying to do a simple email & password authentication. I used one of the db rules from documentation and I don't think I have to create collections and documents in the rules. I've already tested my connection to the db so that's not an issue either. The code I have so far is below:



    import UIKit
    import Firebase


    class SignUpViewController: UIViewController {

    //Outlets
    @IBOutlet weak var firstNameText: UITextField!
    @IBOutlet weak var lastNameText: UITextField!
    @IBOutlet weak var emailText: UITextField!
    @IBOutlet weak var passwordText: UITextField!
    @IBOutlet weak var signUpButton: UIButton!


    override func viewDidLoad() {
    super.viewDidLoad()
    }

    @IBAction func signUpButtonTapped(_ sender: Any) {
    guard let firstName = firstNameText.text,
    let lastName = lastNameText.text,
    let email = emailText.text,
    let password = passwordText.text else { return }

    Auth.auth().createUser(withEmail: email, password: password) { (authResult, error) in
    if let error = error {
    debugPrint("Error creating user: (error.localizedDescription)")
    }
    let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
    changeRequest?.displayName = firstName
    changeRequest?.commitChanges(completion: { (error) in
    if let error = error {
    debugPrint(error.localizedDescription)
    }
    })
    guard let userId = authResult?.user else { return }
    Firestore.firestore().collection(USERS_REF).document(userId).setData([
    USERNAME : firstName,
    DATE_CREATED : FieldValue.serverTimestamp()
    ], completion: { (error) in

    if let error = error {
    debugPrint(error.localizedDescription)
    } else {
    self.dismiss(animated: true, completion: nil)
    }
    })
    }
    }



    }


    But I get the error:




    • Cannot invoke 'setData' with an argument list of type '([String : Any], completion: ((Error?) -> Void)?)'


    Is there something I'm not doing? Could someone help with this email/password authentication?










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm very new to iOS development (learning as I go) and I'm using Cloud Firestore to authenticate users via email/password. I'm trying to do a simple email & password authentication. I used one of the db rules from documentation and I don't think I have to create collections and documents in the rules. I've already tested my connection to the db so that's not an issue either. The code I have so far is below:



      import UIKit
      import Firebase


      class SignUpViewController: UIViewController {

      //Outlets
      @IBOutlet weak var firstNameText: UITextField!
      @IBOutlet weak var lastNameText: UITextField!
      @IBOutlet weak var emailText: UITextField!
      @IBOutlet weak var passwordText: UITextField!
      @IBOutlet weak var signUpButton: UIButton!


      override func viewDidLoad() {
      super.viewDidLoad()
      }

      @IBAction func signUpButtonTapped(_ sender: Any) {
      guard let firstName = firstNameText.text,
      let lastName = lastNameText.text,
      let email = emailText.text,
      let password = passwordText.text else { return }

      Auth.auth().createUser(withEmail: email, password: password) { (authResult, error) in
      if let error = error {
      debugPrint("Error creating user: (error.localizedDescription)")
      }
      let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
      changeRequest?.displayName = firstName
      changeRequest?.commitChanges(completion: { (error) in
      if let error = error {
      debugPrint(error.localizedDescription)
      }
      })
      guard let userId = authResult?.user else { return }
      Firestore.firestore().collection(USERS_REF).document(userId).setData([
      USERNAME : firstName,
      DATE_CREATED : FieldValue.serverTimestamp()
      ], completion: { (error) in

      if let error = error {
      debugPrint(error.localizedDescription)
      } else {
      self.dismiss(animated: true, completion: nil)
      }
      })
      }
      }



      }


      But I get the error:




      • Cannot invoke 'setData' with an argument list of type '([String : Any], completion: ((Error?) -> Void)?)'


      Is there something I'm not doing? Could someone help with this email/password authentication?










      share|improve this question















      I'm very new to iOS development (learning as I go) and I'm using Cloud Firestore to authenticate users via email/password. I'm trying to do a simple email & password authentication. I used one of the db rules from documentation and I don't think I have to create collections and documents in the rules. I've already tested my connection to the db so that's not an issue either. The code I have so far is below:



      import UIKit
      import Firebase


      class SignUpViewController: UIViewController {

      //Outlets
      @IBOutlet weak var firstNameText: UITextField!
      @IBOutlet weak var lastNameText: UITextField!
      @IBOutlet weak var emailText: UITextField!
      @IBOutlet weak var passwordText: UITextField!
      @IBOutlet weak var signUpButton: UIButton!


      override func viewDidLoad() {
      super.viewDidLoad()
      }

      @IBAction func signUpButtonTapped(_ sender: Any) {
      guard let firstName = firstNameText.text,
      let lastName = lastNameText.text,
      let email = emailText.text,
      let password = passwordText.text else { return }

      Auth.auth().createUser(withEmail: email, password: password) { (authResult, error) in
      if let error = error {
      debugPrint("Error creating user: (error.localizedDescription)")
      }
      let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
      changeRequest?.displayName = firstName
      changeRequest?.commitChanges(completion: { (error) in
      if let error = error {
      debugPrint(error.localizedDescription)
      }
      })
      guard let userId = authResult?.user else { return }
      Firestore.firestore().collection(USERS_REF).document(userId).setData([
      USERNAME : firstName,
      DATE_CREATED : FieldValue.serverTimestamp()
      ], completion: { (error) in

      if let error = error {
      debugPrint(error.localizedDescription)
      } else {
      self.dismiss(animated: true, completion: nil)
      }
      })
      }
      }



      }


      But I get the error:




      • Cannot invoke 'setData' with an argument list of type '([String : Any], completion: ((Error?) -> Void)?)'


      Is there something I'm not doing? Could someone help with this email/password authentication?







      ios swift firebase-authentication google-cloud-firestore






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 18:09

























      asked Nov 19 at 12:18









      CadenJoe

      11




      11
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          First check how variables are named properly. In swift we use camelCase.



          Second you have tried to assign your textField object to your password inside dictionary.



          Third you have tried to print and do a segue outside login action.
          Consider editing your code:



            @IBAction func SMLoginTapped(_ sender: Any) {
          print("Login button tapped!")
          guard let emailText = EmailTextField.text, !emailText.isEmpty else { return }
          guard let passwordText = PasswordTextField.text, !passwordText.isEmpty else { return }
          let saveData: [String: Any] = ["email": emailText, "password": passwordText]
          docRef.setData(saveData) { (error) in
          if let error = error {
          print("Oh no! Got an error: (error.localizedDescription)")
          } else {
          print("Data has been saved to Firestore!")
          self.performSegue(withIdentifier: "SecondViewSegue", sender: self)
          }
          }
          }





          share|improve this answer





















          • Thanks man, the projects builds now but it won't login the user. It recognizes the tap (gives "login button tapped" message) but doesn't actually login the user and go to the next segue. Any reason why?
            – CadenJoe
            Nov 20 at 12:27










          • Try using db instead docRef
            – DionizB
            Nov 20 at 15:28










          • I've taken a different approach, but not sure about the above error. I changed the user reference to authResult and then this error came up. Any idea how to troubleshoot it? @DionizB
            – CadenJoe
            Nov 20 at 18:12











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374479%2fcloud-firestore-authentication-ios%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          First check how variables are named properly. In swift we use camelCase.



          Second you have tried to assign your textField object to your password inside dictionary.



          Third you have tried to print and do a segue outside login action.
          Consider editing your code:



            @IBAction func SMLoginTapped(_ sender: Any) {
          print("Login button tapped!")
          guard let emailText = EmailTextField.text, !emailText.isEmpty else { return }
          guard let passwordText = PasswordTextField.text, !passwordText.isEmpty else { return }
          let saveData: [String: Any] = ["email": emailText, "password": passwordText]
          docRef.setData(saveData) { (error) in
          if let error = error {
          print("Oh no! Got an error: (error.localizedDescription)")
          } else {
          print("Data has been saved to Firestore!")
          self.performSegue(withIdentifier: "SecondViewSegue", sender: self)
          }
          }
          }





          share|improve this answer





















          • Thanks man, the projects builds now but it won't login the user. It recognizes the tap (gives "login button tapped" message) but doesn't actually login the user and go to the next segue. Any reason why?
            – CadenJoe
            Nov 20 at 12:27










          • Try using db instead docRef
            – DionizB
            Nov 20 at 15:28










          • I've taken a different approach, but not sure about the above error. I changed the user reference to authResult and then this error came up. Any idea how to troubleshoot it? @DionizB
            – CadenJoe
            Nov 20 at 18:12















          up vote
          1
          down vote













          First check how variables are named properly. In swift we use camelCase.



          Second you have tried to assign your textField object to your password inside dictionary.



          Third you have tried to print and do a segue outside login action.
          Consider editing your code:



            @IBAction func SMLoginTapped(_ sender: Any) {
          print("Login button tapped!")
          guard let emailText = EmailTextField.text, !emailText.isEmpty else { return }
          guard let passwordText = PasswordTextField.text, !passwordText.isEmpty else { return }
          let saveData: [String: Any] = ["email": emailText, "password": passwordText]
          docRef.setData(saveData) { (error) in
          if let error = error {
          print("Oh no! Got an error: (error.localizedDescription)")
          } else {
          print("Data has been saved to Firestore!")
          self.performSegue(withIdentifier: "SecondViewSegue", sender: self)
          }
          }
          }





          share|improve this answer





















          • Thanks man, the projects builds now but it won't login the user. It recognizes the tap (gives "login button tapped" message) but doesn't actually login the user and go to the next segue. Any reason why?
            – CadenJoe
            Nov 20 at 12:27










          • Try using db instead docRef
            – DionizB
            Nov 20 at 15:28










          • I've taken a different approach, but not sure about the above error. I changed the user reference to authResult and then this error came up. Any idea how to troubleshoot it? @DionizB
            – CadenJoe
            Nov 20 at 18:12













          up vote
          1
          down vote










          up vote
          1
          down vote









          First check how variables are named properly. In swift we use camelCase.



          Second you have tried to assign your textField object to your password inside dictionary.



          Third you have tried to print and do a segue outside login action.
          Consider editing your code:



            @IBAction func SMLoginTapped(_ sender: Any) {
          print("Login button tapped!")
          guard let emailText = EmailTextField.text, !emailText.isEmpty else { return }
          guard let passwordText = PasswordTextField.text, !passwordText.isEmpty else { return }
          let saveData: [String: Any] = ["email": emailText, "password": passwordText]
          docRef.setData(saveData) { (error) in
          if let error = error {
          print("Oh no! Got an error: (error.localizedDescription)")
          } else {
          print("Data has been saved to Firestore!")
          self.performSegue(withIdentifier: "SecondViewSegue", sender: self)
          }
          }
          }





          share|improve this answer












          First check how variables are named properly. In swift we use camelCase.



          Second you have tried to assign your textField object to your password inside dictionary.



          Third you have tried to print and do a segue outside login action.
          Consider editing your code:



            @IBAction func SMLoginTapped(_ sender: Any) {
          print("Login button tapped!")
          guard let emailText = EmailTextField.text, !emailText.isEmpty else { return }
          guard let passwordText = PasswordTextField.text, !passwordText.isEmpty else { return }
          let saveData: [String: Any] = ["email": emailText, "password": passwordText]
          docRef.setData(saveData) { (error) in
          if let error = error {
          print("Oh no! Got an error: (error.localizedDescription)")
          } else {
          print("Data has been saved to Firestore!")
          self.performSegue(withIdentifier: "SecondViewSegue", sender: self)
          }
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 at 13:20









          DionizB

          8661313




          8661313












          • Thanks man, the projects builds now but it won't login the user. It recognizes the tap (gives "login button tapped" message) but doesn't actually login the user and go to the next segue. Any reason why?
            – CadenJoe
            Nov 20 at 12:27










          • Try using db instead docRef
            – DionizB
            Nov 20 at 15:28










          • I've taken a different approach, but not sure about the above error. I changed the user reference to authResult and then this error came up. Any idea how to troubleshoot it? @DionizB
            – CadenJoe
            Nov 20 at 18:12


















          • Thanks man, the projects builds now but it won't login the user. It recognizes the tap (gives "login button tapped" message) but doesn't actually login the user and go to the next segue. Any reason why?
            – CadenJoe
            Nov 20 at 12:27










          • Try using db instead docRef
            – DionizB
            Nov 20 at 15:28










          • I've taken a different approach, but not sure about the above error. I changed the user reference to authResult and then this error came up. Any idea how to troubleshoot it? @DionizB
            – CadenJoe
            Nov 20 at 18:12
















          Thanks man, the projects builds now but it won't login the user. It recognizes the tap (gives "login button tapped" message) but doesn't actually login the user and go to the next segue. Any reason why?
          – CadenJoe
          Nov 20 at 12:27




          Thanks man, the projects builds now but it won't login the user. It recognizes the tap (gives "login button tapped" message) but doesn't actually login the user and go to the next segue. Any reason why?
          – CadenJoe
          Nov 20 at 12:27












          Try using db instead docRef
          – DionizB
          Nov 20 at 15:28




          Try using db instead docRef
          – DionizB
          Nov 20 at 15:28












          I've taken a different approach, but not sure about the above error. I changed the user reference to authResult and then this error came up. Any idea how to troubleshoot it? @DionizB
          – CadenJoe
          Nov 20 at 18:12




          I've taken a different approach, but not sure about the above error. I changed the user reference to authResult and then this error came up. Any idea how to troubleshoot it? @DionizB
          – CadenJoe
          Nov 20 at 18:12


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374479%2fcloud-firestore-authentication-ios%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          RAC Tourist Trophy