always getting error Your username or password is incorrect. in cakephp login in xampp code example
Example: always getting error Your username or password is incorrect. in cakephp login in xampp
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');
$this->viewBuilder()->setTheme('Kucing');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'Email',
'password' => 'Password'
]
]
],
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
],
'unauthorizedRedirect' => $this->referer()
]);
$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)
{
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
{
protected function _setPassword($Password)
{
if (strlen($Password) > 0)
{
return (new DefaultPasswordHasher)->hash($Password);
}
}
}