1
votes

I'm using the last version of CakePHP (2.5.4 if I'm right) & I have a problem when trying to login.

I have a table called "Accounts", & my controller is called "UsersController.php", model => "User.php" etc. There seems to be no problem with that, because I'm setting the default table to "Accounts". I have already made a view to signup, and I'm using md5 to hash passwords and it's working perfectly.

But, when I want to login, it ALWAYS returns false & it doesn't log me in.

Here is my login function in UsersController.php:

function login(){
        if($this->request->is('post'))
        {
            if($this->Auth->login())
            {
                $this->Session->setFlash("Vous êtes maintenant connecté !", "notif");
                $this->redirect('/');
            } else {
                $this->Session->setFlash("Les identifiants sont incorrects", "notif", array('type' => 'danger'));
                debug(Security::hash($this->request->data['User']['PasswordHash']));
            }
        }
    }

Here is the form from login.ctp :

<?= $this->Form->input('Login', array('placeholder' => 'Login', 'label' => false)); ?>
                                    <b class="tooltip tooltip-bottom-right">Votre nom de compte</b>
                                </label>
                            </section>
                            <section>
                                <label class="input">
                                    <i class="icon-append fa fa-lock"></i>
                                    <?= $this->Form->input('PasswordHash', array('placeholder' => 'Mot de passe', 'label' => false)); ?>
                                    <b class="tooltip tooltip-bottom-right">Votre mot de passe!</b>
                                </label>
                            </section>
                        </fieldset>  
                        <fieldset>
                            <section>
                                <h4>Mot de passe perdu ?</h4>
                                <p><a style='color:#009900' href="#">Cliquez ici</a> pour réinitialiser le mot de passe.</p>
                            </section>
                        </fieldset>
                    <footer>
                        <center>
                           <?php $options = array(
                                'class' => 'btn-u', 
                                'label' => 'Connexion'
                           ) ?>
                           <?= $this->form->end($options); ?>

I FORGOT TO MENTION THAT MY COLUMNS AREN'T username & password, but rather Login & PasswordHash

Also, here is my AppController.php:

class AppController extends Controller {

public $components = array(
    'Session', 
    'Cookie', 
    'Auth');

function beforeFilter(){
    Security::setHash('md5');
    $this->Auth->allow();
    $this->Auth->authenticate = array(
        'Form' => array(
            'fields' => array('username' => 'Login', 'password' => 'PasswordHash'),
        )
    );
}

}

I can't login and I don't understand why. My request query looks like this :

    SELECT `User`.`Id`, `User`.`Login`, `User`.`PasswordHash`, `User`.`Nickname`, `User`.`Role`, `User`.`AvailableBreeds`, `User`.`Ticket`, `User`.`SecretQuestion`, `User`.`SecretAnswer`, `User`.`Lang`, `User`.`Email`, `User`.`CreationDate`, `User`.`Tokens`, `User`.`NewTokens`, `User`.`LastVote`, `User`.`RecordVersion` FROM `madara`.`accounts` AS `User` WHERE `User`.`Login` = 'Twoast' LIMIT 1

The debug shows me the same hashes as in my table Accounts. I am not sure what I am doing wrong.

Thanks in advance!

2
Have you checked your php error logs on your server? - Mr. Concolato
There is nothing in my PHP error log, it seems that there is no error, the request looks great, but it doesn't want. Damn... - Twoast
Is there any chance you are already logged in? (I think login returns false if you are already logged in.) My login method first checks $this->Auth->loggedIn() is false before anything else is process. - AgRizzo

2 Answers

0
votes

What size have you set the field for password in the table at? Is it long enough to fit the hash field? I had this before and had to set this to at least 50 characters.

0
votes

So, after 3 days, I finally found the solution.

my function login became :

function login(){
        if($this->request->is('post'))
        {
            $data = $this->request->data;
            $Logined = $this->request->data['User']['Login'];
            $PasswordHashed = Security::hash($data['User']['PasswordHash'], 'md5', false);

            $user = $this->User->find('first', array(
                'conditions' => array(
                    'User.Login' => $Logined,
                    'User.PasswordHash' => $PasswordHashed
                ),
                'recursive' => -1
            ));
            if(!empty($user) && $this->Auth->login($user['User'])) {
                $this->Session->setFlash("Vous êtes maintenant connecté !", "notif");
                $this->redirect('/');
            } else {
                $this->Session->setFlash("Les identifiants sont incorrects", "notif", array('type' => 'danger'));
            }
        }
    }

So, this function works well, it just does a manual login.

Thanks for your help ! :)