Cake Php Auth电子邮件或用户名登录

时间:2016-06-22 08:45:54

标签: php cakephp

我目前使用以下设置与Auth组件一起使用,它的工作非常完美,但我的客户现在想让用户选择使用用户名或电子邮件地址登录。

我目前的设置:

AuthComponent::$sessionKey = 'Auth.Customer';

        $this->Auth->allow();

        $this->Auth->authenticate = array(
            'Form' => array(
                'userModel' => 'Customer',
                'fields' => array(
                    'username' => 'email',
                    'password' => 'password',
                ),
                'scope' => array(
                    'Customer.guest' => 0,
                ),
                'passwordHasher' => 'Blowfish',
            )
        );

        $this->Auth->loginAction = array(
            'controller' => 'customers',
            'action' => 'login',
            'admin' => false,
            'plugin' => false,
        );
        $this->Auth->loginRedirect = array(
            'controller' => 'customers',
            'action' => 'my_account',
            'admin' => false,
            'plugin' => false,
        );

        if($this->Auth->user('id') == false){
            $this->Auth->authError = 'Please login to continue';
        }

        $this->Auth->authorize = array('Controller');

我如何才能更改此内容,以便日志需要“电子邮件”或新字段“用户名”。

我会假设一个OR语句但是下面不起作用:

$this->Auth->authenticate = array(
            'Form' => array(
                'userModel' => 'Customer',
                'fields' => array(

                    'password' => 'password',
                    'OR' => array(
                                'username' => 'email',
                                'username' => 'username'
                    )
                ),
                'scope' => array(
                    'Customer.guest' => 0,
                ),
                'passwordHasher' => 'Blowfish',
            )
        );

1 个答案:

答案 0 :(得分:0)

我尝试过更改配置但无法执行此操作。我已经自定义了我的登录识别码,如下所示:

 $findByEmail = $this->Users->findByEmail($this->request->data['username'])->first();
if (isset($findByEmail->username) && $findByEmail->username) {
  $this->request->data['username'] = $findByEmail->username;
 }
  $user = $this->Auth->identify(); 

无论用户名还是电子邮件

,这都是值得的