I found this solution helpful. I created two classes that extend FormAuthenticate:
app / Controller / Component / Auth / ClassNameAuthenticate.php and
<?php App::uses('FormAuthenticate', 'Controller/Component/Auth'); class ClassNameAuthenticate extends FormAuthenticate { }
app / controller / component / Auto / ClassNameEmailAuthenticate.php
<?php App::uses('FormAuthenticate', 'Controller/Component/Auth'); class ClassNameEmailAuthenticate extends FormAuthenticate { }
in my controller added Auth component in the $ components
public $components = array( 'Session', 'Auth' => array( 'authenticate' => array( 'ClassName' =>array( 'userModel'=>'ClassName', 'fields' => array( 'username' => 'username', ), 'scope' => array('ClassName.active' => 1) ), 'ClassNameEmail' =>array( 'userModel'=>'ClassName', 'fields' => array( 'username' => 'email', ), 'scope' => array('ClassName.active' => 1) ) ) ), ); , public $components = array( 'Session', 'Auth' => array( 'authenticate' => array( 'ClassName' =>array( 'userModel'=>'ClassName', 'fields' => array( 'username' => 'username', ), 'scope' => array('ClassName.active' => 1) ), 'ClassNameEmail' =>array( 'userModel'=>'ClassName', 'fields' => array( 'username' => 'email', ), 'scope' => array('ClassName.active' => 1) ) ) ), ); , public $components = array( 'Session', 'Auth' => array( 'authenticate' => array( 'ClassName' =>array( 'userModel'=>'ClassName', 'fields' => array( 'username' => 'username', ), 'scope' => array('ClassName.active' => 1) ), 'ClassNameEmail' =>array( 'userModel'=>'ClassName', 'fields' => array( 'username' => 'email', ), 'scope' => array('ClassName.active' => 1) ) ) ), ); ClassName.active' => public $components = array( 'Session', 'Auth' => array( 'authenticate' => array( 'ClassName' =>array( 'userModel'=>'ClassName', 'fields' => array( 'username' => 'username', ), 'scope' => array('ClassName.active' => 1) ), 'ClassNameEmail' =>array( 'userModel'=>'ClassName', 'fields' => array( 'username' => 'email', ), 'scope' => array('ClassName.active' => 1) ) ) ), ); , public $components = array( 'Session', 'Auth' => array( 'authenticate' => array( 'ClassName' =>array( 'userModel'=>'ClassName', 'fields' => array( 'username' => 'username', ), 'scope' => array('ClassName.active' => 1) ), 'ClassNameEmail' =>array( 'userModel'=>'ClassName', 'fields' => array( 'username' => 'email', ), 'scope' => array('ClassName.active' => 1) ) ) ), ); , public $components = array( 'Session', 'Auth' => array( 'authenticate' => array( 'ClassName' =>array( 'userModel'=>'ClassName', 'fields' => array( 'username' => 'username', ), 'scope' => array('ClassName.active' => 1) ), 'ClassNameEmail' =>array( 'userModel'=>'ClassName', 'fields' => array( 'username' => 'email', ), 'scope' => array('ClassName.active' => 1) ) ) ), ); ClassName.active' => public $components = array( 'Session', 'Auth' => array( 'authenticate' => array( 'ClassName' =>array( 'userModel'=>'ClassName', 'fields' => array( 'username' => 'username', ), 'scope' => array('ClassName.active' => 1) ), 'ClassNameEmail' =>array( 'userModel'=>'ClassName', 'fields' => array( 'username' => 'email', ), 'scope' => array('ClassName.active' => 1) ) ) ), );
login view: login.ctp
<div class="form"> <?php echo $this->Form->create('ClassName'); ?> <fieldset> <legend><?php echo __('Login'); ?></legend> <?php echo $this->Form->input('username'); echo $this->Form->input('password'); ?> </fieldset> <?php echo $this->Form->end(array('label'=>__('Login'))); ?> </div> 'Login')?; <div class="form"> <?php echo $this->Form->create('ClassName'); ?> <fieldset> <legend><?php echo __('Login'); ?></legend> <?php echo $this->Form->input('username'); echo $this->Form->input('password'); ?> </fieldset> <?php echo $this->Form->end(array('label'=>__('Login'))); ?> </div> 'username'); <div class="form"> <?php echo $this->Form->create('ClassName'); ?> <fieldset> <legend><?php echo __('Login'); ?></legend> <?php echo $this->Form->input('username'); echo $this->Form->input('password'); ?> </fieldset> <?php echo $this->Form->end(array('label'=>__('Login'))); ?> </div>
and login ():
public function login(){ if ($this->Auth->loggedIn()) { return $this->redirect($this->Auth->redirect()); } if ($this->request->is('post')) { //Need to duplicate field email for ClassNameEmail Auth $this->request->data['ClassName']['email'] = $this->request->data['ClassName']['username']; if ($this->Auth->login()) { return $this->redirect($this->Auth->redirect()); } $this->Session->setFlash(__('Invalid username/email or password, try again')); } } ClassName'] [ 'email'] = $ this-> request-> data [ 'ClassName'] [ 'username']; public function login(){ if ($this->Auth->loggedIn()) { return $this->redirect($this->Auth->redirect()); } if ($this->request->is('post')) { //Need to duplicate field email for ClassNameEmail Auth $this->request->data['ClassName']['email'] = $this->request->data['ClassName']['username']; if ($this->Auth->login()) { return $this->redirect($this->Auth->redirect()); } $this->Session->setFlash(__('Invalid username/email or password, try again')); } }
I hope someone will find it useful.