Email 使用用户名登录或使用Cakephp 3发送电子邮件

Email 使用用户名登录或使用Cakephp 3发送电子邮件,email,authentication,cakephp-3.0,Email,Authentication,Cakephp 3.0,我想用用户名或电子邮件登录。所以我想动态地更改Auth字段 如何像Cakehp 2那样修改$this->Auth字段 在cakephp 2中,您可以执行以下操作: $this->Auth->authenticate=array( 'Form'=>数组( 'fields'=>array('username'=>'email','password'=>'password'), ), ); 我尝试过这样更改身份验证,但不起作用: $this->Auth->config('authenticate'[

我想用用户名或电子邮件登录。所以我想动态地更改Auth字段

如何像Cakehp 2那样修改$this->Auth字段

在cakephp 2中,您可以执行以下操作:

$this->Auth->authenticate=array(
'Form'=>数组(
'fields'=>array('username'=>'email','password'=>'password'),
),
);
我尝试过这样更改身份验证,但不起作用:

$this->Auth->config('authenticate'[
'表格'=>[
'字段'=>['username'=>'email','password'=>'password']
]
]);

谢谢

我找到了解决办法

我假设用户名是字母数字的(字母和数字)

记住添加
$this->Auth->constructAuthenticate()

AppController.php

使用Cake\Controller\Controller;
使用Cake\Event\Event;
使用Cake\Core\Configure;
类AppController扩展控制器
{
公共函数初始化()
{
父::初始化();
$this->loadComponent('Flash');
$this->loadComponent('Auth'[
“LoginDirect”=>[
“控制器”=>“用户”,
'操作'=>'索引'
],
“logoutRedirect”=>[
“控制器”=>“用户”,
'操作'=>'登录'
]
]);
}
}
UsersController.php

使用App\Controller\AppController;
使用蛋糕\验证\验证;
类UsersController扩展了AppController
{
公共函数登录()
{
如果($this->request->is('post')){
如果(验证::电子邮件($this->request->data['username'])){
$this->Auth->config('authenticate'[
'表格'=>[
'fields'=>['username'=>'email']
]
]);
$this->Auth->constructAuthenticate();
$this->request->data['email']=$this->request->data['username'];
取消设置($this->request->data['username']);
}
$user=$this->Auth->identify();
如果($user){
$this->Auth->setUser($user);
返回$this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(_uu(“无效用户名或密码,请重试”);
}
}
}
login.ctp


这是我的解决方案

AppController.php加载身份验证组件

$this->loadComponent('Auth', [
        'authorize' => 'Controller',
        'authenticate' => [
            'Form' => [
                'userModel' => 'Users',
            ]
        ],
        ...
    ]);
userscocontroller.php

注意:identify()方法

UsersTable.php

假设电子邮件用户名列在同一个表中


使用用户名登录或通过电子邮件发送其完美作品,试试看。

UsersController.php

if($this->request->is('post')){
如果(!filter\u var($this->request->data['username'],filter\u VALIDATE\u EMAIL)==false){
$this->Auth->config('authenticate'[
'表单'=>['fields'=>['username'=>'email','password'=>'password']]
]);
$this->Auth->constructAuthenticate();
$this->request->data['email']=$this->request->data['username'];
取消设置($this->request->data['username']);
}
请购单($this->request->data);
$user=$this->Auth->identify();
如果($user){
$this->Auth->setUser($user);
$this->Flash->success(uu('welcome-'.ucfirst($user['name']));
返回$this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(_uu(“无效用户名或密码,请重试”);
}

这是另一个有用的解决方案(也许)

login.ctp

为了让您的生活更轻松,对于用户输入,只需使用用户名密码


UsersTable.php

这里是我们创建列检查(用户名电子邮件)的地方。 而对于密码检查,它已经由CakePHP负责,不需要在这里包含它

使用Cake\ORM\Query;
公共函数findAuth(查询$Query,数组$options)
{
$query->where([
'或'=>[/$options['username'],//$options['username']//[
'表格'=>[

'finder'=>'auth'//您是否必须向UsersController添加任何其他内容才能使其工作?当我尝试此操作时,我在“if(Validation::email(…)”行上得到“致命错误:类'App\Controller\Validation'not found”。找到它:添加“use Cake\Validation\Validation;”到UsersController的顶部,它就像一个符咒。任何人都可以找到这个链接:@ndm post有答案。只需转到该页面并按Ctrl+F
email
public function login()
{
    if ($this->request->is('post')) {
        //$user = $this->Auth->identify();
        $user = $this->Users->identify($this->request->data);
        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        } else {
            $this->Flash->error(__('Username or password is incorrect'), [
                'key' => 'auth'
            ]);
        }
    }
}
use Cake\Auth\DefaultPasswordHasher;

public function identify($formData) {

        $passOk = false;
        $user = $this->find()->hydrate(false)->where(['email' => $formData['email']])->orWhere(['username' => $formData['email']])->first();
        $checker = new DefaultPasswordHasher;
        if(!is_null($passOk))
            $passOk = $checker->check($formData['password'], $user['password']); 

        return $passOk ? $user  : null;

    }