I cannot log in users in cakePHP 2.4, following the documentation I've created this. It says successfully logged in and also redirects to the destination URL (the users/loggedin action) but $this->Auth->username
and $this->Auth->password
stays blank, no sessions get created. It also gets redirected when trying to log in with wrong credentials. And also tells me the login was successful. I'm clueless now.
AppController.php
class AppController extends Controller {
public $components = array(
'Session',
'DebugKit.Toolbar',
'Auth' => array(
'loginAction' => array('controller' => 'users', 'action' => 'login', 'plugin' => false),
'loginRedirect' => array('controller' => 'users', 'action' => 'loggedin'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'loggedout'),
'authError' => 'UNABLE TO LOG IN.'
)
);
public function beforeFilter(){
$this->Auth->userModel = 'User';
$this->Auth->fields = array('username' => 'username', 'password' => 'password');
$this->Auth->allow('index','display','login','show_reg_form', 'view','signup');
}
}
UsersController.php
I'll only paste the login function:
public function login() {
if ($this->request->is('post')) {
if($this->Auth->login(/*$this->request->data*/)){
$this->Session->setFlash(__('Successfully logged in.'));
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
login.ctp
The relevant part is:
<div class="container-login users form">
<?php echo $this->Form->create('User');
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end(__('Login',true));?>
</div>