Rápli András
Rápli András

Reputation: 3923

Authentication in cakePHP 2.4

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>

Upvotes: 0

Views: 216

Answers (1)

Catalin MUNTEANU
Catalin MUNTEANU

Reputation: 5634

The logged in user details should be read like this:

$id          = $this->Auth->user('id');
$username    = $this->Auth->user('username');
...
$other_field = $this->Auth->user('other_field');

The is no field username in the Auth Object.

You can't read the password field from Auth using user method.

You will need to query the user from the database if you want to get the password. But you should leave the password hashed and alone.

Edit: Since you can access the 'loggedin' page this means that there is a user session created. You just need to change the way you access the user data.

Edit2: Specify the authentication handlers by adding following line to your Auth:

'Auth' => array(
    ....
    'authenticate' => array('Form') 
)

Edit3: Check again that the database field storing the password is VARCHAR with 40 length.

Upvotes: 2

Related Questions