Я использую CakePHP 3.5.Я пытаюсь создать простой логин, но у меня проблемы:
- Я могу войти в систему с паролем, который не был хеширован
- Мой пароль по умолчанию работает.хешируется, но при входе в систему не
- все пользователи (user1, user2, user3) имеют одинаковый пароль «пароль»
- пароль user1 не хешируется
UserController.php
public function login(){
if($this->request->is('post')){
// $data = $this->request->getData();
//pr($data);
$user = $this->Auth->identify();
if($user){
$this->Flash->success('Successful login');
$this->Auth->setUser($user);
return $this->redirect(['action' => 'index']);
}else{
$this->Flash->error(__('Please, try again.'));
}
}
}
<?php
namespace App\Model\Entity;
use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;
/**
* UsersTable Entity
*
* @property int $id
* @property string $username
* @property string $email
* @property string $password
*/
class UsersTable extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* @var array
*/
protected $_accessible = [
'username' => true,
'email' => true,
'password' => true
];
/**
* Fields that are excluded from JSON versions of the entity.
*
* @var array
*/
protected $_hidden = [
'password'
];
protected function _setPassword($password){
return(new DefaultPasswordHasher)->hash($password);
}
}
login.ctp
<?= $this->Form->create();?>
<?= $this->Form->control('email'); ?>
<?= $this->Form->control('password'); ?>
<?= $this->Form->button('login');?>
<?= $this->Form->end(); ?>
AppController.php
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler', [
'enableBeforeRedirect' => false,
]);
$this->loadComponent('Flash');
$this->loadComponent('Auth',[
'authenticate' =>[
'Form' => [
'fields' => [
'username' =>'email',
'password' =>'password'
]
]
],
'loginAction' => [
'controller' =>'UsersTable',
'action' =>'login'
]
]);