2014-06-25 48 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)