Tuesday, February 24, 2015

Re: CakePhp 3.0 unable to login

Try this approach and reminds you better have the names of the tables and columns in English
Default is: Table users and username/password for auth fields.


    public function login()
   
{
        $user
= $this->Users->newEntity();
       
if ($this->request->is('post')) {
            $user
= $this->Users->patchEntity($user, $this->request->data);
            $auth
= $this->Auth->identify();
           
if ($auth) {
                $this
->Auth->setUser($auth);
               
return $this->redirect($this->Auth->redirectUrl());
           
}
            $this
->Flash->error(__('Invalid credentials.'));
       
}
        $this
->set(compact('user'));
   
}




El domingo, 22 de febrero de 2015, 14:05:53 (UTC-2), s.m.s...@gmail.com escribió:

I'm new to CakePhp framework and followed the blog tutorial. Everything went smooth until authentication part.

This is my login method from UsersControllers.php, the debug line always returns false.

public function login()      {          if ($this->request->is('post')) {              debug($this->Auth->identify());              $user = $this->Auth->identify();              if ($user) {                  $this->Auth->setUser($user);                  return $this->redirect($this->Auth->redirectUrl());              }              $this->Flash->error(__('Usuario o contraseña inválida, intente nuevamente'));          }      }

This is my user class I had to change the line protected $_accessible = ['*' => true]; as appears in the tutorial, otherwise users wouldn't be saved.

class User extends Entity  {      // Make all fields mass assignable for now.      protected $_accessible = ['username' => true,'password'=>true,'role'=>true,'created'=>true,'modified'=>true];      // ...      protected function _setPassword($password)      {          return (new DefaultPasswordHasher)->hash($password);      }      // ...  }

This is my login.ctp

<div class="users form">      <?= $this->Flash->render('auth') ?>      <?= $this->Form->create() ?>      <fieldset>          <legend><?= __('Ingrese su usuario y contraseña') ?></legend>          <?= $this->Form->input('Usuario') ?>          <?= $this->Form->input('Contraseña') ?>      </fieldset>      <?= $this->Form->button(__('Login')); ?>      <?= $this->Form->end() ?>  </div>

These are my AppController.php methods:

public function initialize()  {      $this->loadComponent('Flash');      $this->loadComponent('Auth', [      'loginRedirect' => [      'controller' => 'Expedientes',      'action' => 'index'      ],      'logoutRedirect' => [      'controller' => 'Pages',      'action' => 'display',      'home'      ]      ]);  }    public function beforeFilter(Event $event)  {     $this->Auth->allow(['index', 'view']);  }

At this point I can add users, their passwords are hashed but I can't login. Any help will be greatly appreciated. Thanks in advance.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: