Cakephp 3.0身份验证无效

时间:2015-11-07 18:41:04

标签: cakephp-3.0

AppController类

<?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仍然是我收到错误。

1 个答案:

答案 0 :(得分:0)

在AppController.php中更改此内容:

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