Cakephp 2.0 Email Authentication Instead of Username
In my opinion, I have:
<?php echo $this->Form->create('User', array("controller" => "Users", "action" => "login", "method" => "post")); echo $this->Form->input('User.email', array("label" => false)); echo $this->Form->input('User.password', array("label" => false, 'class' => 'password-input')); echo $this->Form->end(); ?>
In my AppController:
public $components = array( 'Session', 'Auth' ); function beforeFilter(){ $this->Auth->fields = array( 'username' => 'email', 'password' => 'password' ); }
In my UserController:
function beforeFilter(){ $this->Auth->allow('sign_up', 'login', 'logout', 'forgot_password'); return parent::beforeFilter(); } public function login() { if ($this->Auth->login()) { $this->Session->setFlash(__('Successfully logged in'), 'default', array('class' => 'success')); $this->redirect($this->Auth->redirect()); } else { if (!empty($this->request->data)) { $this->Session->setFlash(__('Username or password is incorrect'), 'default', array('class' => 'notice')); } } }
But the login does not work, what am I missing?
Thanks.
I believe the problem is this:
function beforeFilter(){ $this->Auth->fields = array( 'username' => 'email', 'password' => 'password' ); }
Thus, CakePHP 1.3 specified user login fields. CakePHP 2.0 instead requires you to specify these fields in public $components = array(...);
. The 1.3 API shows that Auth has the $ fields property, but the 2.0 API shows that the $ fields property no longer exists. Therefore, you must:
public $components = array( 'Session', 'Auth' => array( 'authenticate' => array( 'Form' => array( 'fields' => array('username' => 'email') ) ) ) );
Further information can be found at: http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#configuring-authentication-handlers
Please tell me how it works!