2013-04-18 289 views
0

我無法在cakephp中使用Auth登錄。在stackoverflow上有一些類似的帖子,但是這些答案似乎不適合我。無法使用cakephp登錄

我已經創建了一個類似的註冊表格,可以使用Auth但是登錄, $ this-> Auth-> login();在UsersController中返回false。

驗證使用正確的userModel,並且用戶名字段更改爲電子郵件。 (注意:當我使用用戶名而不是電子郵件時,它也不起作用)。該數據庫有一個表用戶,其中包含一個電子郵件和一個密碼字段。密碼字段被散列/與使用的加密:AuthComponent ::密碼()

// AppController的

class AppController extends Controller { 

    public $helpers = array('Html', 'Form'); 
    public $components = array(
     'Session', 
     'Auth'); 

    public function beforeFilter() { 
     $this->Auth->userModel = 'User'; 
     $this->Auth->fields = array('username' => 'email', 'password' => 'password'); 
     $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login'); 
     $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'index'); 
    } 
} 

// UsersController

class UsersController extends AppController{ 
    var $name = 'Users'; 
    var $helpers = array('Html', 'Form'); 

    function beforeFilter() 
    {  
    parent::beforeFilter(); 
    // tell Auth not to check authentication when doing the 'register' action 
    $this->Auth->allow('register'); 
    } 

    function login(){ 
     if (isset($this->data['User'])) { 
     $this->Auth->login(); 
     debug('tried'); 
     } 
    } 

// login.ctp

<?php 
    echo $this->Form->create('User',array('action'=>'login')); 
    echo $this->Form->input('email'); 
    echo $this->Form->input('password'); 
    echo $this->Form->end('Login'); 

?> 

// user.php

<?php 
class User extends AppModel{ 

    var $name = 'User'; 
} 
?> 

我只使用cakephp幾天,所以這可能是一個簡單的錯誤,但是我現在正在搜索它幾個小時,但我仍然沒有找到它。

+0

您使用哪種CakePHP版本? – ADmad 2013-04-18 06:03:50

回答

1

設置屬性如$this->Auth->userModel$this->Auth->fields在2.x中不會有任何好處。您需要使用$this->Auth->authenticate屬性來指定userModel和字段選項。有關更多信息,請參閱2.x manual關於Auth配置。