I am making a login for a part of a website. Therefore I don’t want to start the Auth in the AppController but in the actual Controller I need it in.
In PremiumController:
[…]
use CakeControllerComponentAuthComponent;
[…]
public function initialize() { parent::initialize(); $this->loadComponent('Auth', [ 'Basic' => ['userModel' => 'Clients'], 'Form' => ['userModel' => 'Clients'], 'loginAction' => [ 'controller' => 'Premium', 'action' => 'login', 'plugin' => false ], 'authError' => 'Unauthorized Access', 'authenticate' => [ 'Form' => [ 'fields' => [ 'username' => 'naam', 'password' => 'pass' ], 'userModel'=>'Clients', //Other table than Users 'passwordHasher' => [ 'className' => 'Premium', //use MD5 ] ] ], 'loginRedirect' => [ 'controller' => 'Premium', 'action' => 'dashboard' ], 'logoutRedirect' => [ 'controller' => 'Search', 'action' => 'index' ] ]); }
[…] and lower the login part
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')); } }
I don’t get any result from this. The $user remains ‘false’
How can I make this work and is there a way to proper debug the Auth component?
Answer
The controller listens to the login-page and his exact request names.
The content of my login.ctp was this
<?= $this->Form->input('username', ['type' => 'text', 'id' => 'username', 'label' => __('Login / E-mail'), 'placeholder' => __('Login / E-mail')]) ?> <?= $this->Form->input('password', ['type' => 'password', 'id' => 'password', 'label' => __('Wachtwoord'), 'placeholder' => __('Wachtwoord')]) ?>
I had to change it into
<?= $this->Form->input('naam', ['type' => 'text', 'id' => 'username', 'label' => __('Login / E-mail'), 'placeholder' => __('Login / E-mail')]) ?> <?= $this->Form->input('pass', ['type' => 'password', 'id' => 'password', 'label' => __('Wachtwoord'), 'placeholder' => __('Wachtwoord')]) ?>
To parse the post-data request to the right fields. My misunderstandig was that the Controller handeled this.