1

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();
?>
Rayann Nayran
  • 1,135
  • 7
  • 15
Sbm
  • 11
  • 6
  • I don't see how that could happen, unless additional involved core code around finders would have been modified/overwritten? What's you exact CakePHP version (last line in `vendor/cakephp/cakephp/VERSION.txt`)? – ndm May 18 '17 at 12:23
  • it is showing 3.2.6 in version.txt – Sbm May 18 '17 at 12:43
  • That's nowhere near the claimed 3.4. Please upgrade to _at least_ the latest 3.2.x release, the `username` option has only been introduced with CakePHP 3.2.9 – ndm May 18 '17 at 13:17
  • @ndm Thanks for this important information ...It was by mistake I have both the version which led to me dig in this error .. – Sbm May 18 '17 at 14:52
  • One more issue I have two tables one for admin and other for users....I need to create single login how I can achieve this so that admin can access only admin authorize pages and user can access user authorize pages...and how do I login with either contact number or email address on username field – Sbm May 18 '17 at 14:53
  • That would be something for a new question. I remember that being asked before though, so you may want to try a quick search first. – ndm May 20 '17 at 11:25

1 Answers1

0
   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

  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Tiago Martins Peres Oct 19 '18 at 10:26