CakePHP - authentication component, using a different table name and column name

前端 未结 2 368
暖寄归人
暖寄归人 2021-01-17 01:42

Just for organization sake, I wanted to use a different table for the authentication component to check, but it doesn\'t quite work. While I can initially state:

相关标签:
2条回答
  • 2021-01-17 02:06

    Make sure your database table "core_users" and model "CoreUser" exists.

    When you setup component you can put login/logout redirect here.

    var $components = array(
            "Auth" => array(
                 'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'),
                 'logoutRedirect' => array('controller' => 'core_users', 'action' => 'login')
                  ),
           "Session");
    

    Now in beforeFilter medhod you can put the following

    function beforeFilter(){
          $this->Auth->authenticate = array(
             AuthComponent::ALL => array('userModel' => 'CoreUser', 'scope' => array("CoreUser.status" => 1), "fields" => array("username" => "email", "password" => "your_password_field"), 'Form', 'Basic'
                                            );
    }
    

    Above example you can ignore status, if you need to pass any other info on the login verification u can use that. Now about you can remove 'Basic' if you only need form validation. I hope this would work .

    0 讨论(0)
  • 2021-01-17 02:08

    First, model names are generally singular. Are you sure you didn't mean CoreUser which would look in the core_users table?

    Secondly, you can use the email field instead of username by setting the fields key on your auth setup.

    public $components = array(
        'Auth' => array(
            'authenticate' => array(
                'Form' => array(
                    'userModel' => 'CoreUser',
                    'fields' => array('username' => 'email')
                )
            )
        )
    );
    

    See the book for more information.

    0 讨论(0)
提交回复
热议问题