如何使用Auth

时间:2014-03-19 16:43:02

标签: php cakephp cakephp-2.3 cakephp-appmodel

我正在尝试在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;
    }
}

UserController.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');
        }
    }
}

的login.php

<h1>Login</h1>

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

1 个答案:

答案 0 :(得分:1)

发现错误。即使表单上的输入字段是&#34;电子邮件&#34;,在Auth中,也会考虑&#34;用户名&#34;

所以

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

转换为此

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