【问题标题】:Auth component not responding身份验证组件没有响应
【发布时间】:2015-07-27 10:23:17
【问题描述】:

在我的例子中,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');
            }
        }

登录.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'));?>

【问题讨论】:

  • cakephp 2.x ,使用 $this->Auth->login() ,使用 login() 不带参数

标签: php cakephp


【解决方案1】:

对 AuthComponent::login 的调用错误

Auth::login method 不检查密码:

如果提供了 $user,该数据将作为登录用户存储。

即不管$this-&gt;data是什么,if it is truthy,这个方法调用总是返回true。

注意与the documentation example的区别:

public function login() {
    if ($this->request->is('post')) {
        // Important: Use login() without arguments! See warning below.
        if ($this->Auth->login()) {

这是 1.x(假设您使用的是 2.x)的行为变化,其中相同的代码尝试从请求数据中识别用户。

【讨论】:

  • 当" if ($this->Auth->login()) { } "提交表单后呈现为空白页。
  • 可能是您的众多 die 语句之一 =)。我建议删除它们并阅读您的应用程序的日志文件。
  • 不,我检查了我的代码,但没有任何 die();
  • 好吧,仅此更改不会导致出现白页 - 您需要调试/调查,例如阅读应用程序的日志文件。
【解决方案2】:

这让人头疼,但终于解决了。当我清除浏览器窗口的 cookie 和缓存时,它没有其他任何东西,它可以正常工作,然后我创建一个注销功能来清除缓存并且它可以工作。

注销():

public function logout() {
    return $this->redirect($this->Auth->logout());
}

但我不明白这个问题也发生在私人窗口中,但私人窗口不存储 cookie,那么为什么这个问题会发生在私人窗口中..

感谢您的建议和支持@AD7six

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多