无法将身份更改为“用户名”;在 Codeigniter Ion Auth 中?

发布于 2024-10-22 13:50:20 字数 3936 浏览 2 评论 0原文

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

ion_auth

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

在控制器

//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()模型中配置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;
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神爱温柔 2024-10-29 13:50:20

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

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

网白 2024-10-29 13:50:20

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

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

冷了相思 2024-10-29 13:50:20

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文