2014-05-19 124 views
0

我无法在cakePHP 2.4中登录用户,请参阅我创建此文档的文档。它说已成功登录,并且也重定向到目标URL(用户/登录操作),但$this->Auth->username$this->Auth->password保持空白,没有会话被创建。尝试使用错误的凭据登录时,它也会被重定向。并且还告诉我登录成功了。我现在无能为力。在cakePHP中进行身份验证2.4

AppController.php

class AppController extends Controller { 
    public $components = array(
     'Session', 
     'DebugKit.Toolbar', 
     'Auth' => array(
      'loginAction' => array('controller' => 'users', 'action' => 'login', 'plugin' => false), 
      'loginRedirect' => array('controller' => 'users', 'action' => 'loggedin'), 
      'logoutRedirect' => array('controller' => 'users', 'action' => 'loggedout'), 
      'authError' => 'UNABLE TO LOG IN.' 
     ) 
    ); 

    public function beforeFilter(){ 
     $this->Auth->userModel = 'User'; 
     $this->Auth->fields = array('username' => 'username', 'password' => 'password'); 
     $this->Auth->allow('index','display','login','show_reg_form', 'view','signup'); 
    } 

} 

UsersController.php

我只粘贴登录功能:

public function login() { 


    if ($this->request->is('post')) { 

      if($this->Auth->login(/*$this->request->data*/)){ 
       $this->Session->setFlash(__('Successfully logged in.')); 
       return $this->redirect($this->Auth->redirectUrl()); 
      } else { 
       $this->Session->setFlash(__('Invalid username or password, try again')); 
      } 
     } 

    } 

login.ctp

相关部分是:

<div class="container-login users form"> 
         <?php echo $this->Form->create('User'); 
          echo $this->Form->input('username'); 
          echo $this->Form->input('password'); 
          echo $this->Form->end(__('Login',true));?> 

        </div> 
+0

'__('Login',true)'应该是__('Login')'。这不再是蛋糕1.3了。 – mark

+0

您是否将参数传递给$ this-> Auth-> login?或者你在这里评论他们? –

+0

我没有通过他们,因为当我上次取消评论时,我可以登录任何内容。我读过文档,它应该没有指定的参数。 –

回答

2

在用户详细信息的记录应该这样写:

$id   = $this->Auth->user('id'); 
$username = $this->Auth->user('username'); 
... 
$other_field = $this->Auth->user('other_field'); 

的是在验证对象没有字段名。

您无法使用用户方法从Auth读取密码字段。

如果您想获取密码,您将需要从数据库中查询用户。但是你应该把密码散开并单独存放。

编辑: 由于您可以访问“登录”页面,这意味着有一个用户会话创建。你只需要改变你访问用户数据的方式。

EDIT2:再次 检查数据库字段存储的密码是VARCHAR 40长度: 通过添加下面一行到你的验证指定的认证处理程序:

'Auth' => array(
    .... 
    'authenticate' => array('Form') 
) 

EDIT3。

+0

以下是这个问题。尝试使用错误的凭据登录时,它也会被重定向。并且还告诉我登录成功了。 –

+0

我应该猜到了吗? –

+0

对不起,更新了这个问题。 –