I'm studying right now CakePHP, and I followed every direction they gave me there and I got wonderful results, but when it comes to the Auth Component (and the very simple authentication they teach at the site), I just can't manage to do it.
The method $this->Auth->login() keeps returning false, and not letting me log in.
I am not using anything more than the tutorial shows me, I'm not even hashing the passwords yet (Calm down, I will, but I need to get the basic before!).
Here goes my code:
Model
App::uses('AppModel', 'Model');
class User extends AppModel {
public $validate = array(
'username' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'required' => true
),
),
'password' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'required' => true
),
),
);
AppController
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'pages', 'action' => 'display', 'home'),
'logoutRedirect' => array('controller' => 'Users', 'action' => 'index')
)
);
}
Users Controller
App::uses('AppController', 'Controller');
class UsersController extends AppController {
public function login()
{
if($this->request->is('post'))
{
if(!$this->Auth->login())
{
$this->Session->setFlash('Invalid Username or Password');
}
else
{
$this->redirect($this->Auth->redirectUrl());
}
}
}
public function logout()
{
$this->redirect($this->Auth->logout());
}
View (Users/login.ctp)
<div class="login-box">
<?php echo $this->Session->flash('auth'); ?>
<?php
echo $this->Form->create();
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end('login');
?>
</div>