Jquery does not validate Incorrect password

Asked

Viewed 226 times

1

I got 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 Check login 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>

When entering, enter normal if the password is correct. If I’m wrong, the message doesn’t come... and I can’t find the error.

1 answer

1


Apparently the login validation in the database is being done in the condition:

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

then if there is no login and password entered, it enters Else.

So I think the program identifies the error in this Else.

To know if you are in this code snippet, you have to print it out in this condition and test it with a login that doesn’t exist. Follow 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 will already know where to change the program so that an error appears when you sign in with an invalid login.

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.