Issue
I know theres a few similar question, but i dont find answer which resolved my problem. My problem is authentication is always false. This is my code:
My AdminController
class AdminController extends AppController
{
public function initialize()
{
parent::initialize();
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'userModel' => 'Admins',
'fields' => [
'username' => 'email',
'password' => 'password'
]
]
],
'loginAction' => [
'controller' => 'Innerlogin',
]
]);
}
}
InnerloginController:
namespace App\Controller\Admin;
use App\Controller\AdminController;
use Cake\Event\Event;
class InnerloginController extends AdminController
{
public function index()
{
$this->loadModel('Admins');
if( $this->request->is('POST') )
{
$admin = $this->Auth->identify();
if( $admin )
{
$this->Auth->setUser( $admin );
return $this->redirect([ 'controller' => 'admin' ]);
}
else
{
$this->Flash->error( 'Incorrect username or password' );
}
}
}
}
Entity/Admin
namespace App\Model\Entity;
use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;
class Admin extends Entity
{
protected $_accessible = [
'*' => true,
'admins_id' => false
];
protected function _setPassword( $password )
{
return( new DefaultPasswordHasher() )->hash( $password );
}
}
Innerlogin/index.ctp
<?= $this->Form->create() ?>
<?= $this->Form->input('username', ['label' => false, 'class' => 'input-cst' ]) ?>
<?= $this->Form->input('password', ['label' => false, 'class' => 'input-cst', 'type' => 'password' ]) ?>
<?= $this->Form->button(__('Login'), ['class' => 'btn btn-success right']) ?>
<?= $this->Form->end() ?>
My admins table
Field Type Null Key Default Extra
admins_id int(11) NO PRI NULL auto_increment
name varchar(40) NO NULL
email varchar(50) NO NULL
password varchar(255) NO NULL
sex varchar(1) NO NULL
birthday varchar(40) NO NULL
created varchar(40) NO NULL
deleted varchar(40) NO 0
Any idea what am i doing wrong ? And by the way i have another related question. I want to make two authentication, one for users another for admins (I dont want to keep role column in one table and check is user or admin). Can i do that? It will be works properly ?
Solution
The fields
option is not a "input field to database column" map, it is used to configure what fields/columns to use instead!
You have configured the form authenticator to use the email
field/column instead of the username
field/column, and so you have to change that in your form accordingly to use email
too, ie
<?= $this->Form->input('email', ['label' => false, 'class' => 'input-cst' ]) ?>
Answered By - ndm
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.