【问题标题】:Unable to change Identity to 'username' in Codeigniter Ion Auth?无法在 Codeigniter Ion Auth 中将身份更改为“用户名”?
【发布时间】:2011-07-18 05:20:37
【问题描述】:

我已经安装了 ion auth,一切正常。我唯一的问题是我想更改登录以使用访问者用户名而不是电子邮件。我更改了 ion_auth.php 配置文件中的 CONFIG 选项,但它仍然不起作用。我是否缺少额外的步骤??

ion_auth 配置

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

控制器中的login()

//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' => '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() 模型

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;
}

【问题讨论】:

  • 它是否适用于电子邮件?贴出选项的代码,贴出你使用的代码。
  • 是的,它确实适用于电子邮件。这是默认安装,默认管理员行通过 mysql 添加。我也创建了几个虚拟帐户。我所做的只是将其更改为“用户名”并尝试登录到 /auth/login 的默认登录屏幕。只是给我一个登录错误。我唯一改变的是配置身份选项。我应该根据该更改创建自己的登录控制器吗?
  • 在这里查看我的答案:stackoverflow.com/questions/12626292/…

标签: php codeigniter authentication ion-auth


【解决方案1】:

为什么您仍在控制器中处理电子邮件(而不是用户名)?

【讨论】:

    【解决方案2】:

    您需要更改您的控制器,因为它仍在从 POST 获取电子邮件并使用它来尝试登录。

    【讨论】:

      【解决方案3】:

      您应该在用户名列的用户表中添加一个索引

      【讨论】:

        猜你喜欢
        • 2013-01-14
        • 2014-05-02
        • 2016-01-23
        • 2013-02-15
        • 1970-01-01
        • 2012-05-30
        • 2013-02-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多