How to authenticate with Cakephp 3?

Asked

Viewed 40 times

1

I’m trying to authenticate with Cakephp, according to what I saw in the framework documentation.

What is currently happening is that my first user, with id 1 enters normally in the application, but all users that I register now, always fail even I typing e-mail and password correctly.

My code is like this:

Form:

<div class="row">
  <?=$this->Form->create()?>
  <div class="form-group">
  <?=$this->Form->control('email', ['class' => 'form-control'])?>
  </div>
  <div class="form-group">
    <?=$this->Form->control('password', ['class' => 'form-control'])?>
  </div>
  <?=$this->Form->button('Entrar', ['class' => 'btn btn-info'])?>
  <?=$this->Html->link(__('Ainda não tem uma conta? Cadastre-se'), 
  ['action' => 'add']);?>
  <?=$this->Form->end()?>
</div>

Login method:

 public function login()
    {
        if ($this->request->is('POST')) {

            $user = $this->Auth->identify();

            if ($user) {
                $this->Auth->setUser($user);
                $this->Flash->success(__('Bem vindo '.$this->Auth->user()['name']));
                return $this->redirect($this->Auth->redirectUrl());
            } else {
                $this->Flash->error(__('Nome de usário ou senha incorretos'));
            }
        }
    }

And finally, the settings in Appcontroller:

$this->loadComponent('Auth', [
            'authenticate' => [
                'Form' => [
                    'fields' => [
                        'username' => 'email',
                        'password' => 'password',
                    ],
                ],
            ],
            'loginRedirect' => [
                'controller' => 'welcome',
                'action' => 'index',
            ],
            'logoutRedirect' => [
                'controller' => 'users',
                'action' => 'login',
            ],
        ]);
        //Permito que seja acessada justamente para fazer os novos cadastros que acabam falhando na autenticação...
        $this->Auth->allow(['add']);

I’m also encrypting the password: (User.php)

public function _setPassword() {
    return (new DefaultPasswordHasher())->hash($password);
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.