简体   繁体   English

CakePHP $ this-> Auth-> login()无效

[英]CakePHP $this->Auth->login() not working

The web app is working great except for auth manual calls. 除了auth手动调用之外,Web应用程序运行良好。 I've been struggling with this for days. 几天来我一直在努力奋斗。 In my sample code I have temporarily rewritten the cookie to narrow down the cause. 在我的示例代码中,我暂时重写了cookie以缩小原因。 Here is my app controller snip: 这是我的app控制器剪辑:

App::import('Sanitize');
//uses('sanitize');
class AppController extends Controller {
    var $components = array('Clean','Acl', 'Auth', 'Session', 'RequestHandler', 'Cookie', /* 'DebugKit.Toolbar' */);
    var $helpers = array('uiNav','Flash','Html', 'Form', 'Session','Javascript','Ajax','Js' => array('Jquery'), 'Time','Js');

    function beforeFilter() {
        //Configure AuthComponent
        $this->Auth->authorize = 'actions'; 
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
        $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'view');
        $this->Auth->actionPath = 'controllers/';
        $this->Auth->autoRedirect = false;

        $this->Auth->allowedActions = array('display');

        if(!$this->Auth->user()){
            //$cookie = $this->Cookie->read('Auth.User');
            $cookie = array('username' => 'chris22', 'password' => 'stuff');
            if (!is_null($cookie)) {
                $this->set('checking_cookie',$cookie);
                if ($this->Auth->login($cookie)) {
                    $this->set('cookie_message','cookie validates!');
                    //  Clear auth message, just in case we use it.
                    $this->Session->delete('Message.auth');
    /*              $this->redirect($this->Auth->redirect()); */
                }
            }
        }
    }
}

As you can see I'm just plugging the user name and password into $this->Auth->login and it's not working!! 正如您所看到的,我只是将用户名和密码插入$ this-> Auth-> login并且它无法正常工作!

I don't know if my user controller is relevent, but here is the login function for that too: 我不知道我的用户控制器是否相关,但这里也是登录功能:

function login() {
    if ($this->Auth->user()) {
        if (!empty($this->data) && $this->data['User']['remember_me'] && isset($this->data['User']['password'])) {
            $cookie = array();
            $cookie['username'] = $this->data['User']['username'];
            $cookie['password'] = $this->data['User']['password'];
            $this->Cookie->write('Auth.User', $cookie, true, '+1 month');
            //unset($this->data['User']['remember_me']);
            $this->set('cookie-00', 'setting cookie.');

        }else{ $this->set('cookie_message', 'not setting cookie.');}

        $this->redirect($this->Auth->redirect());
    }

}    

Thanks! 谢谢!

EDIT - 1 - I think I know why this is not working for you. 编辑 - 1 - 我想我知道为什么这不适合你。

Reason 1: $this->Auth->login takes data in the form of 原因1:$ this-> Auth-> login以数据的形式获取数据

array(
    'User'=>array(
        'username'=>'myusername',
        'password'=>'mypassword'
    )
)

Reason 2: $this->Auth->login does NOT hash the password. 原因2:$ this-> Auth-> login不会对密码进行哈希处理。

You must send the password exactly as it appears in the database. 您必须完全按照数据库中显示的密码发送密码。

EVERYTHING BELOW THIS LINE IS POSSIBLE BUT NOT LIKELY IN THIS CASE: 在这种情况下,这条线路的所有可能性都不大可能:

Are you sure that when you originally created the username and password, the hashes were setup? 您确定在最初创建用户名和密码时,是否设置了哈希值?

  1. To check that your hashes match look at your users table with phpmyadmin or mysql workbench and find the password field for the user chris22 要检查哈希匹配,请使用phpmyadmin或mysql workbench查看users表,并找到用户chris22的密码字段
  2. Compare that entry to your current hashing. 将该条目与当前哈希值进行比较。 To check your current hash, put the code below somewhere in a controller function (index) and navigate there. 要检查当前的哈希值,请将代码放在控制器函数(索引)中的某个位置并在那里导航。

     debug(Security::hash('stuff')); exit; 

I hope this helps! 我希望这有帮助!

Make sure the fields are correctly assigned in the Auth component. 确保在Auth组件中正确分配了字段。 if you use different field names than username & password to connect, you must declare them in your controller like this way : 如果使用不同于用户名和密码的字段名称进行连接,则必须以这种方式在控制器中声明它们:

'Auth' => array(
  'authenticate' => array(
    'Form' => array(
      'fields' => array('username' => 'email', 'password' => 'mot_de_passe')
    )
  )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM