Tôi bắt đầu sử dụng CakePHP 3 sau một thời gian sử dụng CakePHP 2 và tôi gặp khó khăn khi tạo đăng nhập xác thực.Đăng nhập [Auth-> identification()] luôn luôn sai trên CakePHP 3
Chức năng xác thực mới $this->Auth->identify()
luôn trả về false.
Trên cơ sở dữ liệu, mật khẩu được mã hóa hoàn hảo và truy vấn mang người dùng cũng được.
Mã của tôi:
AppController:
[...]
class AppController extends Controller{
public function initialize(){
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'loginRedirect' => [
'controller' => 'Admin',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'Pages',
'action' => 'display'
]
]);
}
public function beforeFilter(Event $event)
{
$this->Auth->allow(['display']);
}
}
UserController:
[...]
class UsersController extends AppController{
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
$this->Auth->allow(['logout']);
}
[...]
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 username or password, try again'));
}
}
[...]
tài khoản (Model Entity):
<?php
namespace App\Model\Entity;
use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;
class User extends Entity{
protected $_accessible = [*];
protected function _setPassword($password){
return (new DefaultPasswordHasher)->hash($password);
}
}
Xem:
<div class="users form">
<?= $this->Flash->render('auth') ?>
<?= $this->Form->create() ?>
<fieldset>
<legend><?= __('Please enter your username and password') ?></legend>
<?= $this->Form->input('username') ?>
<?= $this->Form->input('password') ?>
</fieldset>
<?= $this->Form->button(__('Login')); ?>
<?= $this->Form->end() ?>
</div>
chiều dài của lĩnh vực mật khẩu của bạn là gì? – ADmad
Đó là VARCHAR (255) – Beto
Là 45 .... Đã thay đổi thành 255 và hiện đang làm việc =) Cảm ơn = D – Beto