If you want to perform authentication based on "either" or "basis", you need to use a custom search, since the built-in authenticator search is designed to match only one column.
In your form, define the input:
<?= $this->Form->input('email_or_phone'); ?>
In your auth configuration, configure the fields and finder:
$this->loadComponent('Auth', [ 'authenticate' => [ 'Form' => [ 'fields' => [ 'username' => 'email_or_phone' ], 'finder' => 'auth' ] ] ]);
In your (possibly UsersTable ) table class, define finder, where you can use the username parameter to build the necessary conditions:
public function findAuth(\Cake\ORM\Query $query, array $options) { return $query->where( [ 'OR' => [ $this->aliasField('email') => $options['username'], $this->aliasField('phone') => $options['username'], ] ], [], true ); }
Please note that the value that the authenticator captures from the request through the configured email_or_phone field will always be passed to the finder in the username option!
Also note that you must either remove the possible existing conditions generated by the authenticator or overwrite them as shown in this example using the third argument to Query::where() .
see also
source share