0
votes

Okies this question is similar to one I have asked recently on Stack Overflow, but I'm basically just using the code from the CakePHP Book rather than my own code to try and understand why something is not working.

Basically the idea is to allow a user to login using their email address as well as their username in version 2.0 of Cake. However it always returns that the details are incorrect but I can STILL login with the username, so basically the override in the AppController does not change anything... More so I'm trying to figure out how to allow both fields for logging in.

As discussed in the original post here: Login with email address or username in CakePHP v2.0 @nIcO has put together something that could pontentially work for both fields BUT the issue explained here causes it not to work.

Any ideas? Anyone got email login working with version 2.0.

// AppController

public $components = array(
    'Auth' => array(
        'loginAction' => array(
            'controller' => 'users',
            'action' => 'login'
        ),
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email')
            )
        )
    )
);

// UsersController

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
        }
    }
}
2
What does your login view and User model look like? Can you post that as well? - Oldskool
It's exactly as posted here: book.cakephp.org/2.0/en/tutorials-and-examples/… everything works as I am able to login using the username but NOT the email address. - Cameron

2 Answers

2
votes

Though id added it in UsersController (not in AppController), but this worked for me for email as username:


public $components = array('Auth');

//beforeFilter in UsersController
function beforeFilter() {
   parent::beforeFilter();
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->authenticate = array(
            'Form' => array(                
                'fields' => array('username' => 'email')
            )
        );
}

Hope it helps in some way

1
votes

This is the best implementation I found: http://bin.cakephp.org/view/1831131032

I like how some of the logic has been moved into the Model and clears up the Controller logic and makes it more MVC. Hopefully this will help others.