cakephp 无法登录,$this->Auth->identify()始终为假

qyyhg6bp  于 2022-11-11  发布在  PHP
关注(0)|答案(1)|浏览(163)

$this-〉Auth-〉identify()的另一个问题是始终为假并且无法登录。
已检查的内容:

  • 已为身份验证配置了相应的字段(即电子邮件和密码)
  • 密码字段长度为255个字符
  • 添加用户时,密码被散列,即当前:“我的天哪,我的天哪,我的天哪,我的天哪,我的天哪!”

配置为:

  • Windows 10操作系统
  • XAMPP版本:7.4.12
  • PHP版本7.4.12
  • 嵌入式软件开发(Win 64)PHP/C++编程语言
  • 10.4.16-MariaDB
  • 蛋糕PHP 3.9

我在下面的CMS教程:https://book.cakephp.org/3/en/tutorials-and-examples/cms/authentication.html
AppController.php

//Setting up Authentication
$this->loadComponent('Auth', [
    'authenticate' => [
        'Form' => [
            'fields' => [
                'username' => 'email',
                'password' => 'password'
            ]
        ]
    ],
    'loginAction' => [
        'controller' => 'Users',
        'action' => 'login'
    ],
    'storage' => 'Session',
    // If unauthorized, return them to page they were just on
    'unauthorizedRedirect' => $this->referer()
]);

login.ctp

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

UsersController.php

public function login()
{
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);

            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Flash->error(__('Invalid credentials, try again'));
    }
}

User.php

protected function _setPassword($value)
{
    if (strlen($value)) {
        $hasher = new DefaultPasswordHasher();

        return $hasher->hash($value);
    }
}

SQL结构

CREATE TABLE `users` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `first_name` varchar(100) NOT NULL,
 `last_name` varchar(100) NOT NULL,
 `email` varchar(255) NOT NULL,
 `password` varchar(255) NOT NULL,
 `created` datetime DEFAULT NULL,
 `modified` datetime DEFAULT NULL,
 `created_by` int(11) unsigned DEFAULT NULL,
 `modified_by` int(11) unsigned DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2
bbuxkriu

bbuxkriu1#

您的表单要求输入username,但您的身份验证被配置为使用email域。请将$this->Form->control('username')更改为$this->Form->control('email', ['label' => 'Username'])

相关问题