2
votes

I am building the application using cakephp.

I have choosed the cakephp2.3.

User would be authenticated using either 'email' OR 'username'.

I found one option with Auth component "scope", but with this we can set static conditions.

LIKE: if user is active,, is_active => 1

But I want that while authenticating auth component should check either 'email' or 'username' field and other is password.

Is there any way?

2

2 Answers

1
votes

This needs some code. You can easily find it through Users/CakeDC plugin found here

This plugin Uses the auth component below for login with multiple columns.

https://github.com/CakeDC/users/blob/master/Controller/Component/Auth/MultiColumnAuthenticate.php

It also includes an example of how to use. If you don't want the whole plugin you can just copy the MultiColumnAuthenticate.php to the folder app/Controllers/Components/Auth/.

if you copy only the file then in beforeFilter method inside your AppController you must write:

class AppController extends Controller {

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->authenticate = array(
            'MultiColumn' => array(    //With no plugin
                'fields' => array(
                    'username' => 'username',
                    'password' => 'password'
                ),
                'columns' => array('username', 'email'),
            )
        );
    }
}
0
votes

according to cakephp documentation.

FormAuthenticate allows you to authenticate users based on form POST data. Usually this is a login form that users enter information into

By default AuthComponent uses FormAuthenticate

you can try this:

// Pass settings in $components array
   public $components = array(
   'Auth' => array(
    'authenticate' => array(
        'Form' => array(
            'fields' => array('username' => 'email', 'password' => 'password')
          )
      )
   )
);