在遵循CakePHP简单身份验证教程后尝试登录时,身份验证失败

乌迈尔·汗·贾登(Umair Khan Jadoon)

我正在尝试这个简单身份验证的Cake Cookbook教程。用户创建成功,但是当我尝试登录时,它不会对用户进行身份验证。而是显示“无效的用户名/密码”错误。

我唯一改变的是我使用的是电子邮件地址,而不是用户名。

这是User.php应用程序模型:

<?php
App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');

class User extends AppModel {
    public $validate = array(
        'email' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'An email is required'
            )
        ),
        'password' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A password is required'
            )
        ),
        'role' => array(
            'valid' => array(
                'rule' => array('inList', array('admin', 'author')),
                'message' => 'Please enter a valid role',
                'allowEmpty' => false
            )
        )
    );

    public function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['password'])) {
        $passwordHasher = new BlowfishPasswordHasher();
        $this->data[$this->alias]['password'] = $passwordHasher->hash(
            $this->data[$this->alias]['password']
        );
    }
    return true;
}
}
?>

这是用户控制器(Userscontroller.php)

<?php
App::uses('AppController', 'Controller');

class UsersController extends AppController {

    // public function __construct($request = null, $response = null)
    // {
 //     parent::__construct($request, $response);
    // }

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('add','logout');
    }

    public function index() {
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }

    public function view($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(
                __('The user could not be saved. Please, try again.')
            );
        }
    }

    public function edit($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(
                __('The user could not be saved. Please, try again.')
            );
        } else {
            $this->request->data = $this->User->read(null, $id);
            unset($this->request->data['User']['password']);
        }
    }

    public function delete($id = null) {
        // Prior to 2.5 use
        // $this->request->onlyAllow('post');

        $this->request->allowMethod('post');

        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->User->delete()) {
            $this->Session->setFlash(__('User deleted'));
            return $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('User was not deleted'));
        return $this->redirect(array('action' => 'index'));
    }

    public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Session->setFlash(__('Invalid username or password, try again'));
    }
    }

    public function logout() {
    return $this->redirect($this->Auth->logout());
    }



}
?>

这是login.ctp视图:

<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User'); ?>
    <fieldset>
        <legend>
            <?php echo __('Please enter your username and password'); ?>
        </legend>
        <?php echo $this->Form->input('email');
        echo $this->Form->input('password');
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>

这是Appcontroller.php

<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */

App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package     app.Controller
 * @link        http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */

class AppController extends Controller {
    //...

    public $components = array(
        'Session',
        'Auth' => array(
            'loginRedirect' => array(
                'controller' => 'sites',
                'action' => 'index'
            ),
            'logoutRedirect' => array(
                'controller' => 'pages',
                'action' => 'display',
                'home'
            ),
            'authenticate' => array(
                'Form' => array(
                    'passwordHasher' => 'Blowfish'
                )
            )
        )
    );

    public function beforeFilter() {
        $this->Auth->allow('index', 'view');
    }
    //...
}
kamlesh.bar

在您的Appcontroller.php中编辑以下行,如下所述

 'Form' => array(
            'passwordHasher' => 'Blowfish',
            'fields' => array('username'=>'email', 'password'=>'password'),
        )

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

灰烬简单身份验证:会话在刷新后丢失

来自分类Dev

灰烬简单身份验证承诺

来自分类Dev

Squid中的简单身份验证

来自分类Dev

Azure 简单身份验证 API

来自分类Dev

Ember简单身份验证:身份验证后如何重定向回最后访问的路由?

来自分类Dev

使用 nestjs 身份验证时身份验证失败

来自分类Dev

Ember简单身份验证会话在自定义身份验证器中调用解析后未获得身份验证

来自分类Dev

Ember简单身份验证自定义身份验证器

来自分类Dev

灰烬简单身份验证-自定义授权者破坏身份验证

来自分类Dev

如何使用Ember简单身份验证获取当前经过身份验证的用户?

来自分类Dev

CakePHP 2.5.7-使用非标准用户登录时身份验证失败

来自分类Dev

登录时,WooCommerce REST API身份验证失败

来自分类Dev

使用Laravel 4进行简单身份验证

来自分类Dev

组件的“ Ember简单身份验证” invalidateSession

来自分类Dev

HBase继续进行简单身份验证

来自分类Dev

找不到Ember CLI简单身份验证

来自分类Dev

Go和JWT-简单身份验证

来自分类Dev

找不到Ember CLI简单身份验证

来自分类Dev

组件的“ Ember简单身份验证” invalidateSession

来自分类Dev

AngularJS简单身份验证拦截器

来自分类Dev

尝试上传 Vimeo 视频时身份验证失败

来自分类Dev

MVC 4表单身份验证(无限登录)

来自分类Dev

表单身份验证登录超时推迟

来自分类Dev

Tomcat Realm身份验证-登录后

来自分类Dev

身份验证后,表单身份验证重定向回Login.aspx

来自分类Dev

如何在ASP.NET表单身份验证中启用多次登录尝试?

来自分类Dev

尽管测试成功,表单身份验证仍失败

来自分类Dev

更新后的Ember简单身份验证(Devise),authenticate_with_http_token始终为空

来自分类Dev

重新加载页面后,Ember简单身份验证会话内容丢失

Related 相关文章

  1. 1

    灰烬简单身份验证:会话在刷新后丢失

  2. 2

    灰烬简单身份验证承诺

  3. 3

    Squid中的简单身份验证

  4. 4

    Azure 简单身份验证 API

  5. 5

    Ember简单身份验证:身份验证后如何重定向回最后访问的路由?

  6. 6

    使用 nestjs 身份验证时身份验证失败

  7. 7

    Ember简单身份验证会话在自定义身份验证器中调用解析后未获得身份验证

  8. 8

    Ember简单身份验证自定义身份验证器

  9. 9

    灰烬简单身份验证-自定义授权者破坏身份验证

  10. 10

    如何使用Ember简单身份验证获取当前经过身份验证的用户?

  11. 11

    CakePHP 2.5.7-使用非标准用户登录时身份验证失败

  12. 12

    登录时,WooCommerce REST API身份验证失败

  13. 13

    使用Laravel 4进行简单身份验证

  14. 14

    组件的“ Ember简单身份验证” invalidateSession

  15. 15

    HBase继续进行简单身份验证

  16. 16

    找不到Ember CLI简单身份验证

  17. 17

    Go和JWT-简单身份验证

  18. 18

    找不到Ember CLI简单身份验证

  19. 19

    组件的“ Ember简单身份验证” invalidateSession

  20. 20

    AngularJS简单身份验证拦截器

  21. 21

    尝试上传 Vimeo 视频时身份验证失败

  22. 22

    MVC 4表单身份验证(无限登录)

  23. 23

    表单身份验证登录超时推迟

  24. 24

    Tomcat Realm身份验证-登录后

  25. 25

    身份验证后,表单身份验证重定向回Login.aspx

  26. 26

    如何在ASP.NET表单身份验证中启用多次登录尝试?

  27. 27

    尽管测试成功,表单身份验证仍失败

  28. 28

    更新后的Ember简单身份验证(Devise),authenticate_with_http_token始终为空

  29. 29

    重新加载页面后,Ember简单身份验证会话内容丢失

热门标签

归档