How to use Customizing Find Query for authentication in CakePHP 3.4

521 Views Asked by At

I need to authenticate user using Customizing Find Query for authentication in CakePHP 3.4. I followed cook book for this but in my findAuth method $option returns empty array. I Do not know what is the mistake in the code.

public function initialize()
{
    parent::initialize();
    $this->loadComponent('RequestHandler');
    $this->loadComponent('Auth', [
        'authenticate' => [
            'Form' => [
                'finder' => 'auth',
                'fields' => ['username' => 'username', 'password' => 'password']
            ]
        ],
    ]);
}

UsersTable.php

public function findAuth(\Cake\ORM\Query $query, array $options)
{
    debug($options);
    $query->select(['sno', 'username', 'password','contact'])
        ->where(['Users.username' => $options['username']]);
    return $query;
}

login.ctp

<?php
   echo $this->Form->create();
   echo $this->Form->input('username');
   echo $this->Form->input('password');
   echo $this->Form->submit();
   echo $this->Form->end();
?>
1

There are 1 best solutions below

1
On
   public function findAuth(\Cake\ORM\Query $query, array $options)
    {
        debug($options);
        $query->select(['sno', 'username', 'password','contact'])
            ->where(['Users.username' => $options['username']]);
        return $query;
    }

should be :

public function findAuth(\Cake\ORM\Query $query, array $options)
{
    debug($options);
    $query->select(['sno', 'username', 'password','contact']));
    return $query;
}

references : https://book.cakephp.org/3.0/en/controllers/components/authentication.html#creating-custom-authentication-objects