0
votes

I am working on cakephp 2.x.i have a table in my database name user and it has 4 fields id, email, password and mobileNo

i have two fields in my login.ctp

 <?php


 echo $this->form->create();

echo $this->form->input('email');
echo $this->form->input('password');


echo $this->form->end('submit');
 ?>

what i want is i want to login the user from his mobileNo too(if he typed mobile number rather then email address) just like facebook has done ..he can either login with hi email address or mobileno .i dont want to create another input field.. i dont know how can i do this here is my code

AppController

 class AppController extends Controller {
 public $components = array(
'Session',
'Auth'=>array(
'loginRedirect'=>array('controller'=>'users', 'action'=>'admin'),
'logoutRedirect'=>array('controller'=>'users', 'action'=>'admin'),
'authError'=>"You can't access that page",
'authorize'=>array('Controller'),
  'authenticate' => array(
   'Form' => array(
    'fields' => array('username' => 'email')
    )))
    );
 )
 );


public function isAuthorized($user) {
 }

 public function beforeFilter() {
 $this->Auth->allow('index');
 }
}

UserController

 public function login()
  {
 if ($this->request->is('post')) {
   if ($this->Auth->login()) {
    $this->redirect($this->Auth->redirect());
} else {
    $this->Session->setFlash('Your email/password combination was incorrect');
   }
  }
  }
2

2 Answers

0
votes

i found the solution here https://github.com/ceeram/Authenticate i used plugin to implement this functionality and it works fine ..

0
votes

Use this code in app controller in beforefilter();

AuthComponent::$sessionKey = 'Auth.User';
  if ($this->request->is('post') && $this->action == 'login') {
      $username = $this->request->data['User']['email'];
      if (filter_var($username, FILTER_VALIDATE_EMAIL)) {
          $this->Auth->authenticate = array(
                  'Form' => array(
                      'fields' => array(
                          'username' => 'email', //Default is 'username' in the userModel
                          'password' => 'password'), //Default is 'password' in the userModel
                      // 'scope'=>array('User.status' => '1'),
                      'userModel' => 'User'
                  )
              );
      }else{
       $this->Auth->authenticate['Form']['fields']['email'] = 'mobile';
          $this->request->data['User']['mobile'] = $username;
          unset($this->request->data['User']['email']);
    $this->Auth->authenticate = array(
                  'Form' => array(
                      'fields' => array(
                          'username' => 'mobile', //Default is 'username' in the userModel
                          'password' => 'password'), //Default is 'password' in the userModel
                      // 'scope'=>array('User.status' => '1'),
                      'userModel' => 'User'
                  )
              );

      }
  }

$this->request->data['User']['email'] is my form field in which I am sending email or mobile.