I am trying to implement authentication following the steps in the cookbook however see that the Auth->identify() function is still returning nothing to the $user variable, like credentials are not found. I have a table name called Usuarios and I want to user the fields email and contrasena (password) to authenticate.
Here's what I did:
AppController.php
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => ['finder' => 'auth',
'userModel' => 'Usuarios']
],
'loginAction' => [
'controller' => 'Usuarios',
'action' => 'login'
],
'unauthorizedRedirect' => $this->referer()
]);
$this->Auth->allow(['display']);
UsuariosController.php
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('Tu usuario o contraseña es incorrecta.');
}
}
login.ctp
<h1>Login</h1>
<?= $this->Form->create() ?>
<?= $this->Form->control('email') ?>
<?= $this->Form->control('password') ?>
<?= $this->Form->button('Login') ?>
<?= $this->Form->end() ?>
UsuariosTable.php
public function findAuth(\Cake\ORM\Query $query, array $options)
{
$query
->select(['id', 'email', 'contrasena'])
->where(['Usuarios.estado' => 1]);
return $query;
}
Your help is really appreciated!!
Thanks!