I have a JSON Post authentication to make a login request, of course I need to send the user & password parameters to receive a response for filling my HomeViewController, so I need first to validate the two parameters, and if the user doesn't exist of course I'm going to send an Alert and forbid the access to the Home. The thing is that I don't know in which method do I have to put this validation, because the way I'm doing it throws me an alert before the validation. This is my code:

```
class LoginViewController: UIViewController, LoginProtocol {

    @IBOutlet weak var username: UITextField!
    @IBOutlet weak var password: UITextField!
    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!

    var user : String = ""
    var psw : String = ""
    var idResponse : String = ""
    var message : String = ""
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func validateLogin(_ sender: Any) {
        DoLogin(self.username.text!, self.password.text!)
    }

    @IBAction func login(_ sender: Any) {

        if self.idResponse != "0" {
            validation(message: self.message)
        } else {
            let vc = self.storyboard!.instantiateViewController(withIdentifier: "home") as! HomeViewController
            self.present(vc, animated: false, completion: nil)
            vc.getUser = self.username.text!
            vc.getPassword = self.password.text!
        }
    }

    func validation(message: String) {
        let myAlert = UIAlertController(title: "Alert", message: message, preferredStyle: UIAlertControllerStyle.alert)
        let okAction = UIAlertAction (title: "Ok", style: UIAlertActionStyle.default, handler: nil)
        myAlert.addAction(okAction)
        self.present(myAlert, animated: true, completion: nil)
        return
    }

func DoLogin(_ user:String, _ psw:String)
    {
        let url = URL(string: "http://162.209.99.39:8080/MiClaroBackend/auth")
        let session = URLSession.shared

        let request = NSMutableURLRequest(url: url!)
        request.httpMethod = "POST"

        self.username.text = user
        self.password.text = psw

        let paramToSend = ["username":user,"password":psw]
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = try! JSONSerialization.data(withJSONObject: paramToSend)

        let task = session.dataTask(with: request as URLRequest, completionHandler: {
            (data, response, error) in

            guard let _:Data = data else
            {
                return
            }

            let json:AnyObject?

            do {
                json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
                if let parseJSON = json{
                    let loginModel = LoginModel()

                    let defaultServiceResponse = parseJSON["defaultServiceResponse"] as! NSDictionary
                    loginModel.message = defaultServiceResponse["message"] as! String
                    loginModel.idResponse = defaultServiceResponse["idResponse"] as! Int

                    self.idResponse = String(loginModel.idResponse)
                    self.message = loginModel.message

                    print(json!)

                }
            } catch {
                let error = ErrorModel()
                error.phrase = "PARSER_ERROR"
                error.code = -1
                error.desc = "Parser error in login action"
            }
        })

        task.resume()
    }

```

The validateLogin method is attached to a TextView with the action: "EditingDidEnd". So what I need is that when I finish writing the password, the DoLogin function validates that exists and let me continue to the HomeViewController, but the issue is that when I click the button 'Ingresar'(Login), it shows a blank Alert first and only the second time I click the button it let me through immediately. I don't know why is sending the Alert that I declare at the "func validation" if I'm not calling it before. I'm going to attach a picture to show you the Alert that is appearing:

[![The Alert](https://i.stack.imgur.com/G2cvLl.jpg)](https://i.stack.imgur.com/G2cvLl.jpg)