在我的情况下Auth组件没有正确响应。当我登录错误的邮件地址,它显示错误,但当我登录正确的电子邮件地址,但错误的密码,它不显示错误,重定向到真实的内页。
AppController.php
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'email'
)
),
'loginRedirect' => array(
'controller' => 'users',
'action' => 'index',
)
),
);
UsersController.php
public function login() {
$this->layout = 'login';
if ($this->request->is('post')) {
if ($this->Auth->login($this->data)) {
if (!empty($this->request->data)) {
$access_controll = $this->{$this->modelClass}->find('first', array('conditions'=>array('email'=>$this->request->data['User']['email'])));
if (!empty($access_controll)) {
$role_id = $access_controll['User']['role_id'];
if( ($role_id == 1) OR ($role_id == 2) ) {
return $this->redirect(array('controller' => 'dashboards'));
} else {
die('You are not authenticate person.');
//$this->Session->setFlash(__('You are not authenticate person.'), 'message', array('class' => 'danger'), 'auth');
}
} else {
die ('Something is wrong email id or password.');
//$this->Session->setFlash(__('You are not a register user still.'), 'message', array('class' => 'danger'), 'auth');
}
}
} else {
$this->Session->setFlash(__('invalidLoginDetails'), 'message', array('class' => 'danger'), 'auth');
}
}
login.ctp
<?php echo $this->Form->create('User',array('novalidate'=>'true','inputDefaults' => array('div' => false))); ?>
<?php echo $this->Form->input('email', array('type'=>'email', 'class'=>'form-control', 'placeholder'=>'Email', 'label'=>false)); ?>
<?php echo $this->Form->input('password', array('type'=>'password', 'class'=>'form-control', 'placeholder'=>'Password', 'label'=>false)); ?>
<?php echo $this->Form->end(__('Login'), array('class'=>'btn btn-default btn-block btn-clean'));?>
调用AuthComponent::login
如前所述,Auth::login方法不检查密码:如果提供了$user 该数据将作为登录用户存储。
。无论$this->data
是什么,如果它为真,则此方法调用将始终返回真。
注意与文档示例的区别:
public function login() {
if ($this->request->is('post')) {
// Important: Use login() without arguments! See warning below.
if ($this->Auth->login()) {
这是1的行为变化。X(假设您使用的是2.x),其中相同的代码将尝试从请求数据中识别用户
这让人头疼,但最终解决了。当我清除cookie和浏览器窗口的缓存时,它正常工作,然后我使注销功能清除缓存,它工作。
注销():
public function logout() {
return $this->redirect($this->Auth->logout());
}
但是我不能理解这个问题也发生在私有窗口,但是私有窗口不存储cookie,那么为什么这个问题发生在私有窗口.
谢谢你的建议&支持@AD7six