Auth login with email or mobile Cakephp

后端 未结 2 2012
走了就别回头了
走了就别回头了 2021-01-27 17:42

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 fie

相关标签:
2条回答
  • 2021-01-27 18:08

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

    0 讨论(0)
  • 2021-01-27 18:16

    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.

    0 讨论(0)
提交回复
热议问题