Search code examples
codeigniterauthenticationmulti-user

Multiuser login codeigniter(how to use password_verify method?)


Please help guys, I have encrypted successfully my password with password_hash but is there any solution how to check login and password using PHP password_verify for multiuser login?

here's my controller:

public function index()
{
    $this->form_validation->set_rules('email','Email address','required');
    $this->form_validation->set_rules('password','Password','required');

    if($this->form_validation->run() == FALSE)
    {
        $this->load->view('view_login');    
    } else {
        $this->load->model('Model_members');

        $valid_user = $this->Model_members->check_credential();

        if($valid_user == FALSE)
        {
            $this->session->set_flashdata('error','');
            redirect("login");
        } else {
            $this->session->set_userdata('email', $valid_user->email);          

            if($this->session->userdata('groups') == '1')
            {
                redirect('home');
            }
            elseif($this->session->userdata('groups') == '2')
            {
                redirect('homepage');
            }
            elseif($this->session->userdata('groups') == '0')
            {
                redirect('test1');
            }
        }
    }
}

This is my model:

public function check_credential()
{
    $email = set_value('email');
    $password = set_value('password');

    $hasil3 = $this->db->where('email', $email)
                      ->where('password', $password)
                      ->limit(1)
                      ->get('users');

    if($hasil3->num_rows() > 0)
    {
        return $hasil3->row();
    } else {
        return array();
    }
    }

Very appreciate for the help !!


Solution

  • Please find below mentioned solution, It will help you.

    In Controller

    $userData['email'] = $this->input->post('email');
    $userData['password'] = $this->input->post('password');
    $valid_user = $this->Model_members->check_credential($userData);
    

    In Model your function will look like below.

    public function check_credential($param) {
        $hasil3 = $this->db->where('email', $param['email'])
                ->where('password', password_hash($param['password'], PASSWORD_DEFAULT, ['cost' => 10]))
                ->limit(1)
                ->get('users');
    
        if ($hasil3->num_rows() > 0) {
            return $hasil3->row();
        } else {
            return array();
        }
    }
    

    Let me know if it not work.