Reputation: 69
I have a parse-server app deployed on Heroku and MongoDB. In my Swift 3 app I set a button called register which runs the sign up function:
@IBAction func SignUpBtn(_ sender: UIButton) {
let user = PFUser()
user.username = usernameTextField.text
user.password = passwordTextField.text
user.email = emailTextField.text
user["nameandsurname"] = nameandsurnameTextField.text
user.signUpInBackground {
(succeeded: Bool, error: Error?) -> Void in
if error != nil {
print("error")
self.errorLabel.text = "Please make sure the email has not already been used or try changing the username"
} else {
self.performSegue(withIdentifier: "signuptohome", sender: self)
}
}
When I press the button, even if the fields are blank, the button redirects to the home page anyways, letting the user use the app. How can I do I solve this?
Upvotes: 0
Views: 359
Reputation: 1430
My logic is as follows which works. Also make sure you don't have any Segues
taking place that you slipped in and forgot in your storyboard
.
//this checks 3 fields that they are filled and not empty
if passwordTextField.text == "" || emailTxt.text == "" || usernameTextField.text == ""{
simpleAlert(mess: "You must fill all fields to sign up")
self.hideHUD()
} else {
let user = PFUser()
user.username = usernameTextField.text
user.password = passwordTextField.text
user.email = emailTextField.text
user.signUpInBackground { (succeeded, error) -> Void in
if error == nil {
//success register not lets log him in!!
PFUser.logInWithUsername(inBackground: self.usernameTextField.text!, password:self.passwordTextField.text!) { (user, error) -> Void in
if error == nil {
// Here you can go to your logged in or dashboard page!!
self.hideHUD()
} else {
//failed to log user in
self.simpleAlert(mess: "\(error!.localizedDescription)")
self.hideHUD()
}}
// ERROR
} else {
//This will be the failed to register user
self.simpleAlert(mess: "\(error!.localizedDescription)")
self.hideHUD()
}}
}
Upvotes: 1