0

So, I'm trying to make an authentication in CakePHP that is using email instead of username, which field I don't even have in my users table in database. first i was trying to google that, and i already have tried these: Cakephp 2.0 Authentication using email instead of username Using email instead of username in CakePHP Auth Component and few others, but nothing seems to work - still i get the "incorrect data" error. below is my code

AppController.php

class AppController extends Controller {

public $components = array(
    'Session',
    'Auth' => array(
        'userModel' => 'User',
        'loginRedirect' => array(
            'controller' => 'pages',
            'action' => 'display'
        ),
        'logoutRedirect' => array(
            'controller' => 'pages',
            'action' => 'display',
            'home'
        ),
        'authenticate' => array(
            'Form' => array(
                'fields' => array(
                    'username' => 'email',
                    'password' => 'password'
                ),
                'passwordHasher' => 'Blowfish'
            )
        )
    )
);

public function beforeFilter(){
    $this->Auth->allow('index', 'view');
}

UsersController.php

public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
            $this->request->data['User']['role'] = "user";
            if(strcmp($this->request->data['User']['password'], $this->request->data['User']['repeat']) == 0){
                if ($this->User->save($this->request->data)) {
                    $this->Session->setFlash(__('Użytkownik zarejestrowany.'));
                    return $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
                }
                else $this->Session->setFlash(__('Wystąpił błąd, spróbuj ponownie.'));
            }
            else $this->Session->setFlash(__('Wpisane hasła się nie zgadzają'));
        }
    }

public function login(){
        if($this->request->is('post')){
            if($this->Auth->login()){
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Session->setFlash(__('Nieprawidłowe dane, spróbuj ponownie'.$this->Auth->login()));
        }
    }

login.ctp

<div class="users form">
    <?php echo $this->Session->flash('auth'); ?>
    <?php echo $this->Form->create('User'); ?>
        <fieldset>
            <legend>
                <?php echo __('Zaloguj się'); ?>
            </legend>
            <?php
                echo $this->Form->input('email' /*I also tried with username instead of email here*/, array('label' => 'Email'));
                echo $this->Form->input('password', array('label' => 'Hasło'));
            ?>
        </fieldset>
    <?php echo $this->Form->end(__('Zaloguj')); ?>
</div>

EDIT: Here is User.php

App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');

class User extends AppModel {

    public $validate = array(
        'email' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Email jest wymagany'
            )
        ),
        'password' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Hasło jest wymagane'
            )
        ),
        'repeat' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Powtórz hasło'
            )
        ),
        'identicalFieldValues' => array(
            'rule' => 'identicalFieldValues',
            'message' => 'Wpisane hasła się nie zgadzają'
        )
    );

    public function identicalFieldValues(){
        return $this->data['User']['password'] === $this->data['User']['repeat'];
    }

    public function beforeSave($options = array()){
        if(isset($this->data[$this->alias]['password'])){
            $passwordHasher = new BlowfishPasswordHasher();
            $this->data[$this->alias]['password'] = $passwordHasher->hash(
                $this->data[$this->alias]['password']
            );
        }
        return true;
    }
}

2nd EDIT: I read that the problem can be in size of the varchar in sql, so I changed it from 50 to 130, deleted users, made a new one, but still I cannot log in.

3rd EDIT: I made a completly new, clean cake project, without password hashing, but still with login via email and still im getting "incorrect data" -.-

Community
  • 1
  • 1
Przemek Lewandowski
  • 374
  • 1
  • 2
  • 17
  • Provide code for `User.php`, there you might have included `beforeSave` callback of cakephp, if not include that and go through this link http://book.cakephp.org/2.0/en/core-utility-libraries/security.html – valar morghulis Apr 06 '15 at 13:15
  • Please provide the actual error text or more details about the error itself. – Dave Apr 06 '15 at 15:04
  • Its just returning the setFlash from the login() function – Przemek Lewandowski Apr 06 '15 at 15:28
  • Well, I var dumped the $this->request in login function and what i had there was password just as i typed it, not hashed. i dunno if it should be not hashed there :x – Przemek Lewandowski Apr 07 '15 at 18:16

0 Answers0