CakePHP Auth-> identify()返回false

Bl3akMirai

我正在使用CakePHP,似乎无法使登录正常工作。似乎$ this-> Auth-> identify()不断返回false并不允许我登录。

我已经阅读了有关该问题的所有以前的帖子,但是,没有一篇文章为我提供了解决方案。我尝试登录的用户都是使用cake password hasher创建的,我检查过的用户已经存储在数据库中。我已经检查了数据库中密码字段的长度,该长度设置为varchar(255),已经检查了Auth => authenticate => Form =>字段设置为正确的值(login.ctp字段也正确) )。我也尝试按照某人的建议将$ this-> Form-> control()更改为$ this-> Form-> input(),但是没有运气。

AppController:

 $this->loadComponent('Auth', [
            'loginRedirect' => [
                'controller' => 'Users',
                'action' => 'classes'
            ],
            'logoutRedirect' => [
                'controller' => 'Users',
                'action' => 'index'
            ]
  ]);
$this->loadComponent('Auth', [
                'authenticate' => [
                    'Form' => [
                        'fields' => [
                            'username' => 'email',
                            'password' => 'password'
                        ]
                    ]
                ],
                'loginAction' => [
                    'controller' => 'Users',
                    'action' => 'login'
                ]
        ]);

UsersController中的login()函数:

public function login()
    {
        if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            pj($user);
            if ($user) {
                $this->Auth->setUser($user);
                return $this->redirect(['controller' => 'users']);
            }
            $this->Flash->error(__('Invalid username or password, try again'));
        }
    }

login.ctp:

<div class="users form">
<?= $this->Form->create() ?>
    <fieldset>
        <legend><?= __('Please enter your username and password') ?></legend>
        <?= $this->Form->input('email') ?>
        <?= $this->Form->input('password') ?>
    </fieldset>
<?= $this->Form->button(__('Login')); ?>
<?= $this->Form->end() ?>
</div>

编辑:我忘了补充,我可以成功添加用户,但是我无法登录。

基督教

您正在AppController中两次加载AuthComponent。表单字段配置的第二次加载未加载。

使用所需的配置一次加载组件。

$this->loadComponent('Auth', [
    'authenticate' => [
        'Form' => [
            'fields' => [
                'username' => 'email',
                'password' => 'password'
            ]
        ]
    ],
    'loginRedirect' => [
        'controller' => 'Users',
        'action' => 'classes'
    ],
    'logoutRedirect' => [
        'controller' => 'Users',
        'action' => 'index'
    ],
    'loginAction' => [
        'controller' => 'Users',
        'action' => 'login'
    ]
]);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章