Мне нужно внедрить систему входа в мое приложение, используя CakePHP 3.3.6.
Я использую компонент authenticate, но он не работает должным образом.
Метод $ this-> Auth-> identif (); всегда возвращать false почему?
Вот мой код:
User.php в модели Entity
<?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"> </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"> </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');
}
}
В таблице пользователей есть файлы электронной почты или пароли для проверки учетных данных.