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;
}