【问题标题】:How to use Auth如何使用身份验证
【发布时间】:2014-03-19 16:43:01
【问题描述】:

我正在尝试在 CakePhp 中使用 Auth。我关注了其他一些帖子,但它们似乎都不起作用。这是我所拥有的:

AppController.php

public $components = array(
    'Session',
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'passwordHasher' => 'BlowFish', 
                'fields' => array('email' => 'email', 'password' => 'password')
            )
        ),
        'loginRedirect' => array('controller' => 'poll', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'public', 'action' => 'index'), 
        'flash' => array(
            'element' => 'alert',
            'key' => 'auth',
            'params' => array(
                'plugin' => 'BoostCake',
                'class' => 'alert-error'
            )
        )
    )
);

User.php(模型)

App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');

class User extends AppModel{
    public function beforeSave($option = array()){
        $passwordHasher = new BlowfishPasswordHasher();
        $this->data['User']['password'] = $passwordHasher->hash($this->data['User']['password']);
        return true;
    }
}

用户控制器.php

public function login(){
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirectUrl());
        } else {
            $this->Session->setFlash('Incorrect email and password');
        }
    }
}

登录.php

<h1>Login</h1>

<?php 
    echo $this->Form->create();
    echo $this->Form->input('email');
    echo $this->Form->input('password');
    echo $this->Form->end('Authenticate')
?>

【问题讨论】:

    标签: php cakephp cakephp-2.3 cakephp-model


    【解决方案1】:

    发现错误。尽管表单上的输入字段是“电子邮件”,但在 Auth 中,它被认为是“用户名”

    所以

        echo $this->Form->input('email');
        echo $this->Form->input('password');
    

    翻译成这个

        fields' => array('username' => 'email', 'password' => 'password')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      • 2017-12-16
      • 2017-11-24
      • 2015-08-22
      • 2018-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多