Jquery does not validate password Incorrect

1

I have the following jQuery

$(document).ready(function(){

    $('#email').focus();
    $("#formLogin").validate({
         rules :{
              email: { required: true, email: true},
              senha: { required: true}
        },
        messages:{
              email: { required: 'Campo Requerido.', email: 'Insira Email válido'},
              senha: {required: 'Campo Requerido.'}
        },
       submitHandler: function( form ){  
            var dados = $( form ).serialize();
            $.ajax({
              type: "POST",
              url: "<?php echo base_url();?>entrega/verificarLogin?ajax=true",
              data: dados,
              dataType: 'json',
              success: function(data)
              {
                if(data.result == true){
                    window.location.href = "<?php echo base_url();?>entrega";
                }
                else{
                    $('#call-modal').trigger('click');
                }
              }
              });
              return false;
        },

        errorClass: "help-inline",
        errorElement: "span",
        highlight:function(element, errorClass, validClass) {
            $(element).parents('.control-group').addClass('error');
        },
        unhighlight: function(element, errorClass, validClass) {
            $(element).parents('.control-group').removeClass('error');
            $(element).parents('.control-group').addClass('success');
        }
    });

});

And I have the VerifyLogin method, in CodeIgniter.

public function verificarLogin(){

    $this->load->library('form_validation');
    $this->form_validation->set_rules('email','Email','valid_email|required|xss_clean|trim');
    $this->form_validation->set_rules('senha','Senha','required|xss_clean|trim');
    $ajax = $this->input->get('ajax');
    if ($this->form_validation->run() == false) {

        if($ajax == true){
            $json = array('result' => false);
            echo json_encode($json);
        }
        else{

            $this->session->set_flashdata('error','Os dados de acesso estão incorretos.');
            redirect($this->login);
        }
    } 
    else {

        $email = $this->input->post('email');
        $senha = $this->input->post('senha');

        $this->load->library('encrypt');   
        $senha = $this->encrypt->sha1($senha);

        $this->db->where('email',$email);
        $this->db->where('senha',$senha);
        $this->db->where('status',1);
        $this->db->limit(1);
        $usuario = $this->db->get('usuario')->row();
        $permissao = $usuario->idPermissao;

        $sql = "SELECT * FROM permissoes WHERE idPermissao = '{$permissao}'";
        $permissao = $this->db->query($sql)->row('permissoes');



        if(count($usuario) > 0){




            $dados = array('nome' => $usuario->nome, 'empresa' =>  $usuario->empresa, 'id' => $usuario->idUsuario, 'idCliente' => $usuario->idCliente, 'permissao' => $usuario->idPermissao, 'permissoes' => $permissao, 'logado' => TRUE);
            $this->session->set_userdata($dados);

            if($ajax == true){
                $json = array('result' => true);
                echo json_encode($json);
            }
            else{
                redirect(base_url().'entrega');
            }


        }
        else{


            if($ajax == true){
                $json = array('result' => false);
                echo json_encode($json);
            }
            else{
                $this->session->set_flashdata('error','Os dados de acesso estão incorretos.');
                redirect($this->login);
            }
        }

    }

}

And the HTML:

<form  class="form-vertical" id="formLogin" method="post" action="<?php echo base_url()?>entrega/verificarLogin">
      <?php if($this->session->flashdata('error') != null){?>
            <div class="alert alert-danger">
              <button type="button" class="close" data-dismiss="alert">&times;</button>
              <?php echo $this->session->flashdata('error');?>
           </div>
      <?php }?>
    <div class="control-group normal_text"> <h3><img src="<?php echo base_url()?>assets/img/logo-home.png" alt="Logo" /></h3></div>
    <div class="control-group">
        <div class="controls">
            <div class="main_input_box">
                <span class="add-on bg_lg"><i class="icon-user"></i></span><input id="email" name="email" type="text" placeholder="Email" />
            </div>
        </div>
    </div>
    <div class="control-group">
        <div class="controls">
            <div class="main_input_box">
                <span class="add-on bg_ly"><i class="icon-lock"></i></span><input name="senha" type="password" placeholder="Senha" />
            </div>
        </div>
    </div>
    <div class="form-actions" style="text-align: center">
        <button class="btn btn-info btn-large"/> Logar</button>
    </div>
</form>

At the time of entering, enter normal if the password is right. If I'm wrong, the message does not come ... and I can not find the error.

    
asked by anonymous 06.11.2015 / 17:01

1 answer

1

It appears that the database login validation is being done in the condition:

"if (count ($ user) > 0) {"

So if there is no login and password entered, it will enter the else.

So I think the program identifies the error in that else.

To find out if you are in this code snippet, you have to give a print within that condition and run a test with a login that does not exist. follows an example:

if($ajax == true){

  print('a');

  $json = array('result' => false);
  echo json_encode($json);
} else {

  print('b');

  $this->session->set_flashdata('error','Os dados de acesso estão incorretos.');
  redirect($this->login);
}

If you print "a" or "b", you'll already know where to change the program so that an error is displayed when entering an invalid login.

I hope I have helped.

    
06.11.2015 / 17:49