How to add Email instade of username while login

Hai,

Can anyone clarify me how to add email instead of username while login

  **'fields' => ['username' => 'username', 'password' => 'password']**   ///////This is My code


  **'fields' => ['username' => 'email', 'password' => 'password']** //   I Tried Like that but it wont work for me

any suggestion…?

its correct, is there any error message (debug)? post your complete Auth configuration

This in my login page (UserController)

public function index()
{ 
       $this->layout='mylayout';
		 $users = TableRegistry::get('tabl_reg');
		$query = $users->find();
		$this->set('results',$query);
		
		if($this->request->is('post'))
		{
		 
			$user = $this->Auth->identify();
        
			if($user)
			{
				$this->Auth->setUser($user);
		   
				$session = $this->request->session();
				$session->write('user_id',$user);
				return $this->redirect(array('action' => 'home'));
			} 
			else
			?><script>alert("Your username or password is incorrect");</script><?php
	 
     }
}

But it will show always Alert message (if typing right email & password )
in same case while changing ‘fields’ => [‘username’ => ‘username’, ‘password’ => ‘password’] it is working …

AppController.php

    namespace App\Controller;
    use Cake\Controller\Controller;
    use Cake\Event\Event;
    use Cake\Controller\Component\AuthComponent;
    use Cake\Network\Session\DatabaseSession;

     class AppController extends Controller{
     public function initialize(){
     parent::initialize();
     
     $this->loadComponent('RequestHandler');
     $this->loadComponent('Flash');
     $this->loadComponent('Auth', [
        'authenticate' => [
           'Form' => [
              'fields' => ['username' => 'username', 'password' => 'password']
           ]
        ],
        'loginAction' => ['controller' => 'Users', 'action' => 'index'],
        'loginRedirect' => ['controller' => 'Users', 'action' => 'home'],
        'logoutRedirect' => ['controller' => 'Users', 'action' => 'index']
     ]);
  
     $this->Auth->config('authenticate', [
        AuthComponent::ALL => ['userModel' => 'tabl_reg'], 'Form']);
  }

  public function beforeRender(Event $event){
     if (!array_key_exists('_serialize', $this->viewVars) &&
     in_array($this->response->type(), ['application/json', 'application/xml'])) {
        $this->set('_serialize', true);
     }
  }
}

i think the problem is your user model set in this line:

$this->Auth->config('authenticate', [
        AuthComponent::ALL => ['userModel' => 'tabl_reg'], 'Form']);

remove the line and set in the auth array:

$this->loadComponent('Auth', [
    'loginAction' => ['controller' => 'Users', 'action' => 'index'],
    'loginRedirect' => ['controller' => 'Users', 'action' => 'home'],
    'logoutRedirect' => ['controller' => 'Users', 'action' => 'index'],
    'unauthorizedRedirect' => $this->referer(),
    'authError' => 'Error msg.',
    'authenticate' => [
        'Form' => [
            'userModel' => 'Users', //i belive tabl_reg is the table for model Users correct? so it should be Users here
            //'finder' => 'auth', //here you can create a custom finder if you need return more user information in the auth user session
            'fields' => ['username' => 'email', 'password' => 'password'],
        ]
    ]
]);

I tried but not working…

have you changed your login html form input name? like
<?php echo $this->Form->control('email'); ?>

 echo $this->Form->input('email',array('label'=>'','class'=>' ','placeholder'=>'Enter Your Email','required'=>'true'));
 echo $this->Form->input('password',array('label'=>'','class'=>'' ,'placeholder'=>'Password','required'=>'true')); ?>