2015-04-28 69 views
0

我試圖用POST和驗證但是我UsersController中失敗返回FALSE功能 「這 - $> Auth->確定()」CakePHP的3.X登錄與POST + AUTH失敗

這裏有登錄從AppController中我的設置​​,查看

AppController的

public function initialize() { 
    parent::initialize(); 
    $this->loadComponent('Flash'); 
    $this->loadComponent('Auth'); 
    $this->loadComponent('Cookie'); 
} 

function beforeFilter(Event $event) { 

    $this->Auth->allow('add'); 
    $this->Auth->config('authorize','Controller',true); 
    $this->Auth->config('authenticate', ['Form' => ['fields' => ['username' => 'username', 'password' => 'password']]]); 
    $this->Auth->config('logoutRedirect', 'Homes/index',true); 
    $this->Auth->config('loginRedirect', 'Homes/index',true); 
    $this->Auth->config('loginAction', 'Users/login',true); 
    $this->Auth->config('loginError', 'Usuário e/ou senha incorreto(s)',true); 
    $this->Auth->config('authError', 'Você precisa fazer login para acessar esta página',true); 
    $this->Auth->config('userModel', 'Users',true); 

} 

UsersController

public function login() { 

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

     /* Here return FALSE */ 
     if ($this->Auth->identify()) { 

      $this->Session->setFlash(__('Welcome, ' . $this->Auth->user('username'))); 
     } else { 
      $this->Auth->flash('Login errado'); 
     } 
    } 
} 

兩login.ctp

<?= $this->Form->create(); ?> 
<?php // $this->Form->create('Users'); ?> 
<fieldset> 
    <legend><?= __('Login') ?></legend> 
    <?php 

     echo $this->Form->input('username'); 
     echo $this->Form->input('password'); 
    ?> 
</fieldset> 
<?= $this->Form->button(__('Login')) ?> 
<?= $this->Form->end() ?> 

表的MySQL 用戶名和密碼VARCHAR 200

+0

您的密碼是否被散列並保存正確? –

+0

debug($ this-> request-> data());在您的登錄控制器,並與您的數據庫的密碼進行比較 – CoolLife

回答

0

兩件事情 - 1,這取決於你的數據庫是如何嚴格,你可能必須確保用戶名恰好保存您鍵入它。我在創建像JohnDoe這樣的駱駝的用戶名之前遇到了問題。在升級到MySQL Enterprise之前,鍵入'johndoe'爲我工作,這是非常挑剔的,並且要求我輸入JohnDoe。

2 - 它看起來像您的輸入名稱與數據庫中的字段相匹配,但仔細檢查您是否在設置帳戶時正確保存密碼。我會假設你跟着CakePHP 3 tutorial和密碼被保存,並通過散列實體:

protected function _setPassword($password) 
{ 
    return (new DefaultPasswordHasher)->hash($password); 
} 
// inside the tutorial on the cake site 

繼在蛋糕教程中的說明應爲你工作,並確保跟風與此$ - > Auth-> SETUSER($用戶);之後一旦它返回true。