2015-11-07 77 views
0

AppController的類CakePHP的3.0認證不工作

<?php 
namespace App\Controller; 

use Cake\Controller\Controller; 
use Cake\Event\Event; 
class AppController extends Controller 
{ 

    public function initialize() 
    { 
     parent::initialize(); 

     $this->loadComponent('RequestHandler'); 
     $this->loadComponent('Flash'); 
     $this->loadComponent('Auth', [ 
      'loginRedirect' => [ 
       'controller' => 'dashboard', 
       'action' => 'index' 
      ], 
      'logoutRedirect' => [ 
       'controller' => 'Pages', 
       'action' => 'display', 
       'home' 
      ], 
     'loginAction' => [ 
      'controller' => 'User', 
      'action' => 'login' 
     ], 
     'authenticate' => [ 
      'Form' => [ 
       'fields' => ['email' => 'email', 'password' => 'password'] 
       ], 
     ], 
      'storage' => 'Session' 
     ]); 
    } 

    public function beforeFilter(Event $event) { 
     $this->Auth->allow(['index', 'view', 'display','register']); 
    } 

    public function beforeRender(Event $event) 
    { 
     if (!array_key_exists('_serialize', $this->viewVars) && 
      in_array($this->response->type(), ['application/json', 'application/xml']) 
     ) { 
      $this->set('_serialize', true); 
     } 
    } 
} 

用戶表

<?php 
namespace App\Model\Table; 

use App\Model\Entity\User; 
use Cake\ORM\Query; 
use Cake\ORM\RulesChecker; 
use Cake\ORM\Table; 
use Cake\Validation\Validator; 

/** 
* Users Model 
* 
*/ 
class UsersTable extends Table 
{ 

    /** 
    * Initialize method 
    * 
    * @param array $config The configuration for the Table. 
    * @return void 
    */ 
    public function initialize(array $config) 
    { 
     parent::initialize($config); 

     $this->table('users'); 
     $this->displayField('name'); 
     $this->primaryKey('userid'); 

     $this->addBehavior('Timestamp'); 

    } 

    /** 
    * Returns a rules checker object that will be used for validating 
    * application integrity. 
    * 
    * @param \Cake\ORM\RulesChecker $rules The rules object to be modified. 
    * @return \Cake\ORM\RulesChecker 
    */ 

    public function validationDefault(Validator $validator) 
    { 
     $validator 
      ->add('userid', 'valid', ['rule' => 'numeric']) 
      ->allowEmpty('userid', 'create'); 

     $validator 
      ->requirePresence('name', 'create') 
      ->notEmpty('name'); 

     $validator 
      ->add('email', 'valid', ['rule' => 'email']) 
      ->requirePresence('email', 'create') 
      ->notEmpty('email') 
      ->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']); 

     $validator 
      ->requirePresence('password', 'create') 
      ->notEmpty('password'); 


     $validator 
      ->requirePresence('phone_no', 'create') 
      ->notEmpty('phone_no'); 


    } 
    public function buildRules(RulesChecker $rules) 
    { 
     $rules->add($rules->isUnique(['email'])); 
     return $rules; 
    } 
} 

Login.ctp

<div class="users form"> 
<?= $this->Flash->render('auth') ?> 
<?= $this->Form->create() ?> 
    <fieldset> 
     <legend><?= __('Please enter your username and password') ?></legend> 
     <?= $this->Form->input('username') ?> 
     <?= $this->Form->input('password') ?> 
    </fieldset> 
<?= $this->Form->button(__('Login')); ?> 
<?= $this->Form->end() ?> 
</div> 

UserController中

public function login() 
{ 
    if ($this->request->is('post')) { 
     $user = $this->Auth->identify(); 
     if ($user) { 
      $this->Auth->setUser($user); 
      return $this->redirect($this->Auth->redirectUrl()); 
     } 
     $this->Flash->error(__('Invalid username or password, try again')); 
    } 
} 

我無法登錄我剛收到錯誤消息無效的用戶名或密碼,請重試。密碼長度大於255仍然出現錯誤。

+0

在這方面的專家,我不認爲這是你的問題,但[認證文件(HTTP://book.cakephp .org/3.0/en/controllers/components/authentication.html)表示您可以配置要使用的用戶名和密碼字段的名稱,但您已嘗試配置電子郵件字段。然後你繼續前進,仍然在模板中使用用戶名字段。也許你對文檔的審查與你所做的相比將會產生其他問題並解決這個問題。 –

回答

0

改變這在你的AppController.php:

'authenticate' => [ 
     'Form' => [ 
      'fields' => ['username' => 'email', 'password' => 'password'] 
      ],