Jonathan Cunza Jonathan Cunza - 3 months ago 11
PHP Question

Not validate user cakephp 3

I will see that such a login where I followed all the steps according to the documentation with the only difference that my login is another pun other than the default layout that is within my project structure I have something


\app\src\Tempalte\layout\default.ctp public.ctp


well up there my layouts now the auth component

class AppController extends Controller {
$this->loadComponent('Auth',[
'authorize' => ['Controller'],
'authenticate' => [
'Form' => [
'userModel' => 'Usuarios',
'fields' => [
'username' => 'usuario',
'password' => 'password'
]
]
],
'loginAction' => [
'controller' => 'Usuarios',
'action' => 'login'
],
'authError' => 'Ingrese Datos',
'loginRedirect' => [
'controller' => 'Portal',
'action' => 'dashboard'
],
]);
}


my controller user

public function login() {
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Flash->error('Usuario y/o clave inválido',['key' => 'auth']);
}
}
$this->viewBuilder()->layout('public');
}


my layout public that would come to be my login

<?= $this->Flash->render('auth') ?>
<?= $this->Form->create() ?>
<div class="form-group">
<div class="input-group">
<i class="input-group-addon icon-user"></i>
<?= $this->Form->text('usuario',['placeholder' => 'Usuario','class'=>'form-control']) ?>
</div>
</div>
<div class="form-group">
<div class="input-group">
<i class="input-group-addon icon-key"></i>
<?= $this->Form->password('password',['placeholder' => 'Contraseña','class'=>'form-control']) ?>
</div>
</div>
<div class="form-group">
<?= $this->Html->link('Olvidaste tu Contraseña','/usuarios/password');?>
<?= $this->Form->button('Ingresar',['class'=>'btn btn-inverse btn-right'])?>
</div>
<?= $this->Form->end()?>


load everything but always sends the message Usuario y/o clave inválido

this is my table

CREATE TABLE IF NOT EXISTS `usuario` (
`id_usuario` int(11) NOT NULL,
`usuario` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;


I was not supposed to miss more with that should work or whatever is doing wrong please help

Answer

Please check the following:

  1. You password field in the database table seems to be too short: varchar(50). It needs to be at least 60 characters long or better 255 to ensure the more comprehensive hashers would always work.
  2. Check that the user record has been created with the proper hasher.
  3. Ensure you put a call to $this->loadComponent('Auth') inside the initialize() method of your AppController. E.g.:

    class AppController extends Controller {
        public function initialize()
        {
            parent::initialize();
            $this->loadComponent('Auth', [...]);
        }
    }