Good morning, I’m newbie in CakePHP 3.8.2
I was create authenticate, but always return failed while Email and Password true
below in sampe code
in AppController
<?php
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');
//panggil plugin kucing
$this->viewBuilder()->setTheme('Kucing');
//authenticate
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'Email',
'password' => 'Password'
]
]
],
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
],
// If unauthorized, return them to page they were just on
'unauthorizedRedirect' => $this->referer()
]);
// izinkan mode membaca
$this->Auth->allow(['beranda', 'view', 'index']);
}
public function beforeFilter(Event $event)
{
$this->Auth->allow(['beranda', 'view', 'index']);
}
public function beforeRender(Event $event)
{
parent::beforeFilter($event);
$this->set('userInfo', $this->Auth->user());
}
public function isAuthorized($user)
{
//admin full akses
if(isset($user['Role']) && $user['Role'] === 'Administrator') {
return true;
} else {
return false;
}
}
}
in UsersController
<?php
namespace App\Controller;
use App\Controller\AppController;
use App\Model\Table\UsersTable;
use Cake\Event\event;
use Cake\I18n\Time;
use Cake\ORM\TableRegistry;
class UsersController extends AppController
{
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
$this->Auth->allow(['logout', 'add']);
}
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect(['controller' => 'Articles', 'action' => 'index']);
}
$this->Flash->error('Email dan password salah, Coba lagi');
}
}
public function logout()
{
$this->Flash->success('Anda berhasil logout');
return $this->redirect($this->Auth->logout());
}
}
in Entity/User.php
<?php
namespace App\Model\Entity;
use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;
class User extends Entity
{
//hash password
protected function _setPassword($Password)
{
if (strlen($Password) > 0)
{
return (new DefaultPasswordHasher)->hash($Password);
}
}
}
while register new user, default hasher password success working, but when I’m try to login with the same Email and Password the result authenticate return false.
thanks to someone for help me,…