Cakephp Custom Finder for Auth, with related model
<p>First, this question builds on the question and answer here:  <a href="https://stackoverflow.com/questions/32661318/modifying-authcomponent-identify-method/32669470#32669470">Modifying AuthComponent identify method</a></p>

<p>What I'm trying to implement is token based access to API using Basic Auth over SSL.  I have the API prefix setup and working, and I've successfully used Basic authentication AS LONG AS the fields defined for username &amp; password are within the Tokens table, as below:</p>

<pre><code>public function initialize() {
    parent::initialize();
    $this-&gt;loadComponent('RequestHandler');
    $this-&gt;loadComponent('Auth', [
        'authenticate' =&gt; [
            'Basic' =&gt; [
                'fields' =&gt; ['username' =&gt; 'user_id', 'password' =&gt; 'app_key_hashed'],
                'userModel' =&gt; 'Tokens'
            ]
        ],
        'storage' =&gt; 'Memory',
        'unauthorizedRedirect' =&gt; false
    ]);
}
</code></pre>

<p>Ultimately I would like to authenticate using the User's email (From the Users model) AND the app_key (from the Tokens model).  After reading the documentation (and the above linked question and answer), I feel as though I need to implement this using a custom finder.  So I change the code above to read:</p>

<pre><code>public function initialize() {
    parent::initialize();
    $this-&gt;loadComponent('RequestHandler');
    $this-&gt;loadComponent('Auth', [
        'authenticate' =&gt; [
            'Basic' =&gt; [
                'fields' =&gt; ['username' =&gt; 'user.email', 'password' =&gt; 'app_key_hashed'],
                'userModel' =&gt; 'Tokens',
                'finder' =&gt; 'auth'
            ]
        ],
        'storage' =&gt; 'Memory',
        'unauthorizedRedirect' =&gt; false
    ]);
}
</code></pre>

<p>And include the following in my TokensTable.php:</p>

<pre><code>    /**
 * Custom finder for Auth
 */

public function findAuth(\Cake\ORM\Query $query, array $options) {
    $query-&gt;select(['id', 'user_id', 'app_key_hashed', 'Users.email'])
            -&gt;where(['Tokens.inactive' =&gt; 0])
            -&gt;contain(['Users']);

    return $query;
}
</code></pre>

<p>When I do this, the return message is this:  </p>

<pre><code>{
  "message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column    'Tokens.user.email' in 'where clause'",
  "url": "/flight-ops/api/tokens",
  "code": 500
}
</code></pre>

<p>I feel as though the issue isn't in the custom finder finding the data, but in the filtering of the data by the Auth component.  Am I correct?  Am I missing something simple?  Am I barking up the right tree using a custom finder?</p>

<p>I'm using CakePHP 3.2.4.</p>

<p>Thanks for your help!</p>

<p>Here are my Tables and relationships:</p>

<pre><code>class TokensTable extends Table
{

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config)
{
    parent::initialize($config);

    $this-&gt;table('tokens');
    $this-&gt;displayField('id');
    $this-&gt;primaryKey('id');

    $this-&gt;addBehavior('Timestamp');

    $this-&gt;belongsTo('Users', [
        'foreignKey' =&gt; 'user_id'
    ]);
}
}

class UsersTable extends Table
{

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config)
{
    parent::initialize($config);

    $this-&gt;table('users');
    $this-&gt;displayField('id');
    $this-&gt;primaryKey('id');

    $this-&gt;addBehavior('Timestamp');

    $this-&gt;hasOne('Tokens', [
        'foreignKey' =&gt; 'user_id'
    ]);
}
}
</code></pre>