1
votes

The default login process requires e-mail, not username.

I've added this to AppController.php on beforeFilter(), which is also mentioned on CakePHP's Docs Authentication:

$this->Auth->fields = array(
    'username' => 'username',
    'password' => 'password'
);

But somehow it does not allow users to login with their usernames. Any ideas on how I can change that?

AppController

App::uses('Controller', 'Controller');
class AppController extends Controller {
var $components = array(
    'Session',
    'RequestHandler',
    'Security'
);
var $helpers = array('Form', 'Html', 'Session', 'Js');

public function beforeFilter() {
    $this->Auth->authorize = 'Controller';
    $this->Auth->fields = array('username' => 'username', 'password' => 'password');
    $this->Auth->loginAction = array('plugin' => 'users', 'controller' => 'users', 'action' => 'login', 'admin' => false);
    $this->Auth->loginRedirect = '/';
    $this->Auth->logoutRedirect = '/';
    $this->Auth->authError = __('Sorry, but you need to login to access this location.', true);
    $this->Auth->loginError = __('Invalid e-mail / password combination.  Please try again', true);
    $this->Auth->autoRedirect = true;
    $this->Auth->userModel = 'User';
    $this->Auth->userScope = array('User.active' => 1);  
    if ($this->Auth->user()) {
        $this->set('userData', $this->Auth->user());
        $this->set('isAuthorized', ($this->Auth->user('id') != ''));
    }
}

/View/Users/login.ctp

This is the same as the default login.ctp found in the plugin's folder. I just changed the field email to username.

Now, here is something interesting. Regardless the code I put in this file, CakePHP pulls the content from the plugin login view, the view I created is ignored.

Debugging

When calling the debugger:

Debugger::dump($this->Auth);

It shows all the values that I set. But it still does not accept the username. I can still login with email, so it is not that I am inserting the wrong credentials. It is just that it is waiting for email/password, not username/password.

1
untested/not sure if this is the correct method, but the Cake DC plugin sets those values around line 144 in Users\Controller\UsersController.php - you could try adjusting them there. - Ross
The thing is, I don't want to change the core and I don't think this is the right way to override settings. But if there is no other option, I'll have to follow this route. - rlcabral
Something to keep in mind, the CakeDC Users plugin uses that email for registration, forgot password...etc etc. - Dave
@Dave, I am aware. I just want to use username to login. All the other features can use the user email. If the plugin isn't that flexible, I'll have to drop it. I still want to allow users to signup via Facebook/twitter/Google... and it seems it will be a hassle to integrate it with this plugin. - rlcabral

1 Answers

3
votes

Try configuring it on the $components in AppController:

public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'username')
            )
        )            
    )
}

I had the problem on the inverse way, I wanted to validate users with their mail, and not username, and using the above configuration with 'username' => 'email' worked for me.

EDIT:

public $components = array(
    'Acl',
    'Auth' => array(
        'authorize' => array(
            'Actions' => array('actionPath' => 'controllers')
        ),
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email')
            )
        )            
    ),
    'Session'
);

public function beforeFilter() {
    //User settings
    $this->activeUserId = $this->Auth->user;
    $this->set('activeuserphoto', $this->Auth->user('photo'));
    $this->set('activeusername', $this->Auth->user('username'));

    //Configure AuthComponent
    $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
    $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
    $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'dashboard');
}