Auth login with email or mobile Cakephp

最后都变了- 提交于 2019-12-20 06:16:20

问题


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');
   }
  }
  }

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/17121118/auth-login-with-email-or-mobile-cakephp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!