0
votes

i'm a beginner in CakePHP and I get a problem and I don't know if this is a bug. I can login only after register the user, if I do the logout and try to login again it doesn't work, it says that my username or password is wrong :( and it's right T.T and I have sure that the data is in the DB. It's very strange and because of this I always have to register a new user to access my system. My code is below:

AppController:

class AppController extends Controller {
   public $components = array(
        'Session',
        'Auth' => array(
            'loginRedirect' => array('controller' => 'users', 'action' => 'lista_tudo'),
            'logoutRedirect' => array('controller' => 'users', 'action' => 'lista_tudo'),
            'authorize' => array('Controller')
        )
    );

    function beforeFilter() {
        $this->Auth->allow('login','add');
    }
    public function isAuthorized($user) {
        return true; // Admin pode acessar todas actions
}
}

UsersController:

    public $uses = array('User','UserGroup','Group');
    public $name = "Users";
    public $helpers = array('Html','Form','Meu');
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('logout','login','add');
    }
    public function login() {
        var_dump($this->Auth->loggedIn());
        if($this->request->is('post')){
            if ($this->Auth->login()) {
                $this->Session->setFlash('Logado com sucesso');
                $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash('Usuário e/ou senha inválido');
            }
        }
    }
    public function logout() {
        $this->redirect($this->Auth->logout());
    }
}

Login view

<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User');?>
    <fieldset>
        <legend><?php echo __('Please enter your username and password'); ?></legend>
        <?php echo $this->Form->input('username');
        echo $this->Form->input('password');
    ?>
    </fieldset>
<?php echo $this->Form->end('Login');?>
<?php echo $this->Html->link('Logout',array('action'=>'logout')); ?>
</div>

User Model:

public $name='User';
        public $hasMany = array(
                'UserGroup'=>array(
                        'className'=>'UserGroup'
                    )
            );
        public $validate = array(
            'username'=>array(
                    'rule'=>'isUnique',
                    'message'=>'Usuário já existe'
                )
        );
        public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
        }
        return true;
        }
1

1 Answers

1
votes

I take it you login automatically after registering - so you don't have to enter your username and password to login at this point.

Can you confirm this and paste your registration form view and controller code?

The alias for the password looks wrong in the model, normally you would use something like:

$this->Form->echo('pwd');

In your register form and then:

public function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['pwd'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['pwd']); // Move hashed value from pwd to password
    }
    return true;
}

In your Model.

See also this excellent article: http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/