$ this-> Auth-> identify();在cakephp 3.6.6中始终返回false值

时间:2018-07-05 11:23:42

标签: php authentication cakephp cakephp-3.0

我需要使用CakePHP 3.3.6在我的应用程序中实现登录系统 我使用了身份验证组件,但无法正常工作。

方法$ this-> Auth-> identify();总是返回false为什么?

这是我的代码:

实体模型中的User.php

<?php
namespace App\Model\Entity;

use Cake\ORM\Entity;
use Cake\Auth\DefaultPasswordHasher;

class User extends Entity
{
    protected $_accessible = [
        'email' => true,
        'password' => true,
        'date_created' => true
    ];

    protected $_hidden = [
        'password'
    ];

    protected function _setPassword($password){
        if (strlen($password) > 0) {
            return (new DefaultPasswordHasher)->hash($password);
        }
    }
}

LoginController:

<?php
namespace App\Controller;

use Cake\Core\Configure;
use Cake\Http\Exception\ForbiddenException;
use Cake\Http\Exception\NotFoundException;
use Cake\View\Exception\MissingTemplateException;
use Cake\Event\Event;

class LoginController extends AppController 
{
   public function index(){
      if($this->request->is('post')){
        $user = $this->Auth->identify();
        if($user){
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        }
        else{
            $this->Flash->error('Incorrect Login');
        }
      }
   }
}

index.ctp:

    <div class="row">
        <div class="columns large-3">&emsp;</div>
        <div class="panel columns large-6">
            <div class="text-center">
                <h3>Login</h3>
            </div>
            <?= $this->Form->create(); ?>
                <?= $this->Form->input('email'); ?>
                <?= $this->Form->input('password',['type'=>'password']); ?>
                <?= $this->Html->link('Do not have an account?',['controller'=>'login','action'=>'register'],['class'=>'pull-right']) ?>
                <?= $this->Form->submit('Login',['class'=>'button']); ?>
            <?= $this->Form->end(); ?>
            <?= $this->Flash->render() ?>
        </div>
        <div class="columns large-3">&emsp;</div>
    </div>

AppController:       

    namespace App\Controller;

    use Cake\Controller\Controller;
    use Cake\Event\Event;

    class AppController extends Controller
    {

        public function initialize()
        {
            parent::initialize();

            $this->loadComponent('RequestHandler', [
                'enableBeforeRedirect' => false,
            ]);
            $this->loadComponent('Flash');
            $this->loadComponent('Security');
            $this->loadComponent('Csrf');
            $this->loadComponent('SessionsActivity');
            $this->loadComponent('Auth',[
                'authenticate' =>[
                    'Form' => [
                        'fields' => [
                            'email' => 'email',
                            'password' =>'password'
                        ]
                    ]
                ],
                'loginAction' => [
                    'controller' => 'Login',
                    'action' => 'index'
                ],
                'storage' => 'Session',
            ]);
            $this->loadComponent('Security');
        }
    }

在“用户”表中,有用于验证凭据的电子邮件或密码字段。

0 个答案:

没有答案