我在尝试设置AuthComponent时完全迷失了方向。 每次登录都失败。
这是我的AppController beforeFilter
功能:
public function beforeFilter() {
$this->Auth->authenticate = array(
'all' => array(
'userModel' => 'ClientUser',
'fields' => array(
'username' => 'login',
'password' => 'password'
)
)
);
$this->Auth->loginAction = array('controller' => 'client_users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'static', 'action' => 'clientcenter');
$this->Auth->logoutRedirect = array('controller' => 'static', 'action' => 'home');
// I deny stuff later on
$this->Auth->allow();
}
这是ClientUsers控制器中的login
函数:
public function login() {
// Check login data
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
}
}
它总是失败。我不明白为什么。
这是我的$request->data
内容:(我实际上使用“登录”和“用户名”作为字段名称,没有工作)
ClientUser
login: user@email.com
password: thepassword
客户端密码在模型中进行散列,使用authcomponent(在脚本顶部导入。我之前使用了安全散列函数,但这也没有用):
public function beforeSave($options) {
$this->data['ClientUser']['password'] = AuthComponent::password($this->data['ClientUser']['password1']);
return true;
}
答案 0 :(得分:1)
你在哪里验证适配器? 正如http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#authentication-objects
所述// at least one adapter is necessary (here Form)
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
);