2014-06-25 45 views
0

我最近一直在澆在驗證組件一些CakePHP的教程,和我使用此以供參考:http://miftyisbored.com/a-complete-login-and-authentication-application-tutorial-for-cakephp-2-3/CakePHP的2.3.1驗證組件

我的蛋糕的版本是2.3.1

我使用Firebug進行調試。數據庫密碼是正確的散列,我可以註冊,新用戶就好,但登錄是一個不同的故事。當我嘗試訪問需要身份驗證的頁面時,我得到登錄頁面,輸入有效的用戶/密碼,並將其重定向到登錄頁面。在螢火蟲POST標頭是200行,並只是再次返回登錄頁面。我猜測我的會話配置或某事可能有問題,就像會話沒有被查找一樣。這裏是我的UsersController.php:

<?php 
class UsersController extends AppController { 
    public $name = 'Users'; 
    public $helpers = array('Html','Form'); 

    public function beforeFilter() { 
      parent::beforeFilter(); 
      $this->Auth->allow('add', 'login'); 
     } 


    public function login() { 

      //if already logged-in, redirect 
      if($this->Session->check('Auth.User')){ 
       $this->redirect(array('controller'=>'admin','action' => 'admin_index'));  
      } 

      // if we get the post information, try to authenticate 
      if ($this->request->is('post')) { 
       if ($this->Auth->login()) { 
        $this->Session->setFlash(__('Welcome, '. $this->Auth->user('username'))); 
        $this->redirect($this->Auth->redirectUrl()); 
       } else { 
        $this->Session->setFlash(__('Invalid username or password')); 
       } 
      } 
     } 


    public function logout() { 
     return $this->redirect($this->Auth->logout()); 
    } 

    public function index() { 
     $this->User->recursive = 0; 
     $this->set('users', $this->paginate()); 
    } 

    public function view($id = null) { 
     $this->User->id = $id; 
     if (!$this->User->exists()) { 
      throw new NotFoundException(__('Invalid user')); 
     } 
     $this->set('user', $this->User->read(null, $id)); 
    } 

    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' => 'login')); 
      } 
      $this->Session->setFlash(
       __('The user could not be saved. Please, try again.') 
      ); 
     } 
    } 

    public function edit($id = null) { 
     $this->User->id = $id; 
     if (!$this->User->exists()) { 
      throw new NotFoundException(__('Invalid user')); 
     } 
     if ($this->request->is('post') || $this->request->is('put')) { 
      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.') 
      ); 
     } else { 
      $this->request->data = $this->User->read(null, $id); 
      unset($this->request->data['User']['password']); 
     } 
    } 

    public function delete($id = null) { 
     $this->request->onlyAllow('post'); 

     $this->User->id = $id; 
     if (!$this->User->exists()) { 
      throw new NotFoundException(__('Invalid user')); 
     } 
     if ($this->User->delete()) { 
      $this->Session->setFlash(__('User deleted')); 
      return $this->redirect(array('action' => 'index')); 
     } 
     $this->Session->setFlash(__('User was not deleted')); 
     return $this->redirect(array('action' => 'index')); 
    } 

} 
?> 

我的模型似乎是工作好,但我可以提供任何額外的代碼,如果這不是在我的錯誤是在!謝謝一束!

回答

0

問題是在你的密碼的encryptacion,看看你正在做一個beforesave,你需要做一個加密保存,並且您登錄到驗證自己是否在您的數據庫相同的密碼時

用戶模型

public function beforeSave($options = array()) { 
    // hash our password 

    if (!$this->id) { 
     $passwordHasher = new SimplePasswordHasher(); 
     $this->data['User']['password'] = $passwordHasher->hash($this->data['User']['password']); 
    } 
    return true; 
} 

AppController的

public function beforeFilter() { 
    Security::setHash('sha1'); 
    $this->Auth->allow('index', 'display', 'view'); 
} 

在你的數據庫中輸入密碼右邊是varchar(30)