dsieyx2015 2018-07-05 11:23
浏览 39

$这 - > Auth->标识(); 在cakephp 3.6.6中总是返回false值

I need to implement a login system in my application using CakePHP 3.3.6 I use authenticate component but it does not work properly.

The method $this->Auth->identify(); always return false why?

Here is my code:

User.php in Entity model

<?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');
        }
    }

In the users table have email or password fileds for verify credentials.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
    • ¥15 linux驱动,linux应用,多线程
    • ¥20 我要一个分身加定位两个功能的安卓app
    • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
    • ¥15 IAR程序莫名变量多重定义
    • ¥15 (标签-UDP|关键词-client)
    • ¥15 关于库卡officelite无法与虚拟机通讯的问题
    • ¥15 目标检测项目无法读取视频
    • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
    • ¥100 求采集电商背景音乐的方法