I cannot login using email in CakePHP 2.1, when i try with username then its working perfectly good.
Below is the code
View : login.ctp
<?php
echo $this->Form->create();
echo $this->Form->input('email');
//echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end('Login');
?>
Controller : UsersController.php
<?php
public function login(){
if ($this->request->is('post')){
if ($this->Auth->login()){
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid email and password'));
}
}
}
?>
Model : User.php
<?php
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
public function beforeSave(){
if (isset($this->data[$this->alias]['password'])){
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
}
?>
Database Structure : users table
id Auto_Increment
username
email
password
role
I dont know, why it is not accepting any email id for login but for username its working good.
Read this section of the CakePHP book.
Here is the example from that section, you’ll need to configure the form authentication to use a different field for the username.