Kuresto
Kuresto

Reputation: 3

CakePHP: Auth Component not logging in

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>

Upvotes: 0

Views: 292

Answers (1)

jimmymadon
jimmymadon

Reputation: 612

You will have to hash the password before saving it into the database. When authenticating using AuthComponent::login(), the password entered in your login form is first hashed and then checked with the entry in the database. As your entry in the database is not hashed, the check returns false.

Hash the password in User::beforeSave() as mentioned here in the book.

Upvotes: 5

Related Questions