phpcakephpauthenticationcakephp-2.4

Cakephp 2.4 Authentication


I'm using Cakephp 2.4 and I'm having trouble with the Auth for a simple authentication.

I use the column "mail" in my db as login.

What I do:

in AppController.php

public $components = array('DebugKit.Toolbar', 'Session', 'Auth' => array(
        'loginAction' => array(
            'controller' => 'users',
            'action' => 'login',
            'plugin' => false
        ),
        'authError' => 'Pensiez-vous réellement que vous étiez autorisés à voir cela ?',
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'mail')
            )
        )
    ));

in UsersController.php

public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                return $this->redirect($this->Auth->redirectUrl());
            } else {
                $this->Session->setFlash(
                    __('Username ou password est incorrect'),
                    'default',
                    array(),
                    'auth'
                );
            }
        }
    }

in login.ctp

<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User');?>
    <fieldset>
        <legend><?php echo __('Merci de rentrer votre nom d\'user et mot de passe'); ?></legend>
        <?php echo $this->Form->input('username');
        echo $this->Form->input('password');
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Connexion'));?>
</div>

I tried with a clear and hashed password but every time he won't log me.


Solution

  • Looks like you're using the wrong form field... here's my code, which is working in one of my applications:

    Controller:

    public $components = array(
        'Auth'=> array(
            'authenticate' => array(
                'Form' => array(
                    'fields' => array('username' => 'email')
                )
            )
        )
    );
    

    View:

    echo $this->Form->create();
        echo $this->Form->input('email');
        echo $this->Form->input('password');
    echo $this->Form->end(__('Sign in', true));
    

    Please notice that I'm using a "email" input in my form, and not "username" which is wrong.