2014-10-03 155 views
0

我是CakePHP的新手我試着按照樣例身份驗證並在CakePHP文檔中登錄,我仍然無法登錄。CakePHP無法登錄身份驗證

我在別人看到同樣的問題,但我試圖像它一樣工作,但沒有爲我工作。即使我提供了正確的憑據,我也無法登錄,但$this->Auth->login()正在返回false。

這裏是我的代碼

<?php 
//UsersController 
public function add() { 
     if ($this->request->is('post')) { 
      $this->User->create(); 
      if ($this->User->save($this->request->data)) { 
       $this->Session->setFlash(__('The user has been saved')); 
       return $this->redirect(array('action' => 'index')); 
      } 
      $this->Session->setFlash(
       __('The user could not be saved. Please, try again.') 
      ); 
     } 
    } 

public function beforeFilter() { 
      parent::beforeFilter(); 
      // Allow users to register and logout. 
      $this->Auth->allow('add', 'logout'); 
      parent::beforeFilter(); 
     } 


public function login() { 
      if ($this->request->is('post')) { 
       debug($this->Auth->login()); 
       if ($this->Auth->login()) { 
        return $this->redirect($this->Auth->redirect()); 
       } 
       $this->Session->setFlash(__('Invalid username or password, try again')); 
      } 
     } 

//User Model 
public $validate = array(
     'username' => array(
      'required' => array(
       'rule' => array('notEmpty'), 
       'message' => 'A username is required' 
      ) 
     ), 
     'password' => array(
      'required' => array(
       'rule' => array('notEmpty'), 
       'message' => 'A password is required' 
      ) 
     ), 
     'role' => array(
      'valid' => array(
       'rule' => array('inList', array('admin', 'author')), 
       'message' => 'Please enter a valid role', 
       'allowEmpty' => false 
      ) 
     ) 
    ); 


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


// 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')); ?> 
</div> 

?> 
+0

您不允許用戶登錄而是註銷。雖然你應該允許登錄但不能登出 – Abhishek 2014-10-03 05:24:26

回答

0

更改此功能:

public function beforeFilter() { 
    parent::beforeFilter(); 
    // Allow users to register and logout. 
    $this->Auth->allow('add', 'logout', 'login'); //Add login 
} 
0

更新你的AppController像

public $components = array(
    'Auth' => array(
     'authenticate' => array(
      'Form' => array(
       'passwordHasher' => 'Blowfish' 
      ) 
     ) 
    ) 
); 
2

看來,你沒有申報驗證組件。你可以通過簡單地引用我的示例代碼來實現。

public $components = array('Auth' => array('authenticate' => array(
      'Form' => array(
       'userModel' => **YOUR MODEL** 
      ) 
     ), 
     'loginAction' => array(
      'controller' => **YOUR CONTROLLER**, 
      'action' => 'login' 
       ), 
     'loginRedirect' => array(
      'controller' => **YOUR CONTROLLER**, 
      'action' => 'index' 
     ), 
     'logoutRedirect' => array(
      'controller' => **YOUR CONTROLLER**, 
      'action' => 'login' 
     ) 
    ) 
); 

您可能需要先閱讀文檔或閱讀他們的博客教程,因爲這在第一次啓動時幫助了我很多。 :)

Cakephp's Documentation