开发者

Unable to change Identity to 'username' in Codeigniter Ion Auth?

I have installed ion auth and everything is up and functional. The only problem I have is I want to change the login to use the visitors username instead of e-mail. I change the CONFIG option in the ion_auth.php config file and it still doesnt work. Is there an extra step Im missing??

ion_auth config

/**
 * A database column which is used to
 * login with.
 **/
$config['identity']            = 'username';

login() in the controller

//log the user in
function login()
{
    $this->data['title'] = "Login";

    //validate form input
    $this->form_validation->set_rules('email', 'E-mail Address', 'required|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'required');

    if ($this->form_validation->run() == true)
    { //check to see if the user is logging in
        //check for "remember me"
        $remember = (bool) $this->input->post('remember');

        if ($this->ion_auth->login($this->input->post('email'), $this->input->post('password'), $remember))
        { //if the login is successful
            //redirect them back to the home page
            $this->session->set_flashdata('message', $this->ion_auth->messages());
            redirect($this->config->item('base_url'), 'refresh');
        }
        else
        { //if the login was un-successful
            //redirect them back to the login page
            $this->session->set_flashdata('message', $this->ion_auth->errors());
            redirect('auth/login', 'refresh'); //use redirects instead of loading views for compatibility with MY_Controller libraries
        }
    }
    else
    {  //the user is not logging in so display the login page
        //set the flash data error message if there is one
        $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');

        $this->data['email'] = array('name' => 'email',
            'id' =开发者_如何学C> 'email',
            'type' => 'text',
            'value' => $this->form_validation->set_value('email'),
        );
        $this->data['password'] = array('name' => 'password',
            'id' => 'password',
            'type' => 'password',
        );

        $this->load->view('auth/login', $this->data);
    }
}

login() model

public function login($identity, $password, $remember=FALSE)
{
    if (empty($identity) || empty($password) || !$this->identity_check($identity))
    {
    return FALSE;
    }

    $query = $this->db->select($this->identity_column.', id, password, group_id')
              ->where($this->identity_column, $identity)
              ->where('active', 1)
              ->where($this->ion_auth->_extra_where)
              ->limit(1)
              ->get($this->tables['users']);

    $result = $query->row();

    if ($query->num_rows() == 1)
    {
    $password = $this->hash_password_db($identity, $password);

    if ($result->password === $password)
    {
        $this->update_last_login($result->id);

        $group_row = $this->db->select('name')->where('id', $result->group_id)->get($this->tables['groups'])->row();

        $session_data = array(
                $this->identity_column => $result->{$this->identity_column},
                'id'                   => $result->id, //kept for backwards compatibility
                'user_id'              => $result->id, //everyone likes to overwrite id so we'll use user_id
                'group_id'             => $result->group_id,
                'group'                => $group_row->name
                 );

        $this->session->set_userdata($session_data);

        if ($remember && $this->config->item('remember_users', 'ion_auth'))
        {
        $this->remember_user($result->id);
        }

        return TRUE;
    }
    }

    return FALSE;
}


Why are you still processing email in the controller (instead of username)?


You need to change your controller since it is still grabbing email from POST and using it to try to login.


You should add an index in the users table of username column

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜