授权登录与电子邮件或移动Cakephp



我正在开发cakephp 2.x。我有一个表在我的数据库名称用户,它有4个字段id,电子邮件,密码和mobileNo

我有两个字段在我的登录。ctp

 <?php

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

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

我想要的是我想从他的手机登录用户也(如果他输入手机号码而不是电子邮件地址),就像facebook做的…他可以登录与他的电子邮件地址或手机号码。我不想创建另一个输入字段。我不知道该怎么做这是我的代码

有一个

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

用户控件

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

我找到了解决方案https://github.com/ceeram/Authenticate我使用插件来实现这个功能,它工作得很好。

在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']是我发送电子邮件或手机的表单字段。

最新更新