2015-02-06 68 views
0

我是CakePHP的初學者,我遇到了一個問題,我不知道這是否是一個錯誤。 我只能在註冊用戶後登錄,如果我註銷並嘗試再次登錄它不起作用,它說我的用戶名或密碼是錯誤的:(它是正確的TT並且我確信數據是在。將DB這是非常奇怪的,因爲這樣我總是要註冊一個新用戶訪問我的系統 我的代碼如下:cakephp中的登錄錯誤(或不)

的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()); 
    } 
} 

登錄次數

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

用戶模型:

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

我想你註冊後自動登錄 - 所以你不必輸入用戶名和密碼,在這一點上登錄。

你能否確認這一點並粘貼你的註冊表單視圖和控制器代碼?

密碼的別名看起來錯誤的模型,通常你會使用類似:

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

在您的註冊表單,然後:

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

在你的模型。

另請參閱這篇優秀的文章:http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/