CodeIgniter Login function form not working

Viewed 58

I have sum up this is my code for my login function, I'm using CodeIgniter framework. I am trying to make users login with their email and password. I'm trying to use bcrypt hash password to login but I must be doing something wrong in my coding because it is not logging and it keeps showing invalid.

public function login()
{

    if ($this->session->userdata('logged_in') == 1 
        && in_array($this->session->userdata('user_type'), ['Admin', 'Member']) {
            redirect('dashboard', 'location');
    }

    $this->form_validation->set_rules('username', 'Username', 'trim|required');
    $this->form_validation->set_rules('password', 'Password', 'trim|required');


    if ($this->form_validation->run() == false) {
        $this->login_page();
    } else {
        $this->csrf_token_check();

        $username = ($this->input->post('username', true));
        $password = ($this->input->post('password', true));
          
        $table = 'users';

        if($this->config->item('master_password') != '') {
            if ($_POST['password']) {
                $where['where'] = array('email' => $username); //master password
            } else {
                $where['where'] = array('email' => $username, 'password' => $password);
            }
        } else {
            $where['where'] = array('email' => $username, 'password' => $password);
        }


        $info = $this->basic->get_data($table, $where, $select = '', $join = '', $limit = '', $start = '', $order_by = '', $group_by = '', $num_rows = 1);

        $count = $info['extra_index']['num_rows'];

        if ($count == 0) {
            $this->session->set_flashdata('login_msg', $this->lang->line("invalid email or password"));
            redirect(uri_string());
        } else {
            $username = $info[0]['name'];

            if ($logo=="") {
                $logo=base_url("assets/img/avatar/avatar-1.png");
            } else {
                $logo=base_url().'member/'.$logo;
            }

            $this->session->set_userdata('logged_in', 1);
            $this->session->set_userdata('username', $username);

            $this->basic->insert_data('user_login_info',$login_info_insert_data);

            $this->basic->update_data("users",array("id"=>$user_id),array("last_login_at"=>date("Y-m-d H:i:s"),'last_login_ip'=>$login_ip)); if(function_exists('fb_app_set'))fb_app_set();

            if ($this->session->userdata('logged_in') == 1 
                && in_array($this->session->userdata('user_type'), ['Admin', 'Member']) {
                redirect('dashboard', 'location');
            }
        }
    }
}
1 Answers

This answer is specific to CI3, as you have not mentioned the version.

First, I think it's weird you're using $username for email field in DB. you can correct your naming conventions.

I think this piece of code must be giving you problems

if($this->config->item('master_password') != '')
            {
                if(($_POST['password']))
                $where['where'] = array('email' => $username); //master password
                else $where['where'] = array('email' => $username, 'password' => $password);
            }
            else $where['where'] = array('email' => $username, 'password' => $password);

if you're using the above $where directly in CI as $this->db->where($where), Your code should be

if($this->config->item('master_password') != '')
            {
                if(($_POST['password']))
                $where = array('email' => $username); //master password
                else $where = array('email' => $username, 'password' => $password);
            }
            else $where = array('email' => $username, 'password' => $password);

Notice, I removed where key from the $where array.

Also, if you're using bcrypt to store password in db, you have to fetch the row first and then compare the password like this:

    $where = array('email' => $username);
    $info = $this->basic->get_data($table, $where, $select = '', $join = '', $limit = '', $start = '', $order_by = '', $group_by = '', $num_rows = 1);

    if ($this->bcrypt->check_password($password, $info[0]['password'])) {
        //We're good, login to the system
    } else {
        //Wrong password
    }

I think, your other code, like $this->basic->get_data function should also be reviewed. To debug, try putting var_dump($info[0]); after this function, you'll know if you have a the row or not.

Also, you can use $str = $this->db->last_query(); to print out the last query run.

Related