CakePHP $ this-> Auth-> login()返回false

时间:2013-08-07 01:35:00

标签: cakephp authentication login frameworks

我使用Cake提供的哈希创建了一个用户..但是当我登录时,说“用户名或密码无效”。但没关系。

$this->Auth->login();始终返回false ...

Crontroller

class MastersController extends AppController{

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

AppController的

class AppController extends Controller {

    public $components = array('Session', 'Cookie', 'Auth');
    function beforeFilter() {
        $this->loadModel('Master');

        $this->Auth->userModel = 'Master';
        $this->Auth->allow('*');
        // Action da tela de login
        $this->Auth->loginAction = array(
                'masters' => false,
                'controller' => 'masters',
                'action' => 'login'
        );

        // Action da tela após o login (com sucesso)
        $this->Auth->loginRedirect = array(
                'masters' => true,
                'controller' => 'masters',
                'action' => 'index'
        );

        // Action para redirecionamento após o logout
        $this->Auth->logoutRedirect = array(
                'masters' => false,
                'controller' => 'pages',
                'action' => 'login'
        );
        $this->Auth->authorize = array('controller');
        if (!isset($this->params['masters']) || !$this->params['masters'])
            $this->Auth->allow('*','login');

        $this->Auth->loginError = __('Usuário e/ou senha incorreto(s)', true);
        $this->Auth->authError = __('Você precisa fazer login para acessar esta página', true);
    }

    public function isAuthorized($masters){
        return TRUE;
    }}

查看login.ctp

 echo 'Formulário de login';
echo $this->Session->flash('auth');
echo $this->Session->flash();
echo $this->Form->create('Master', array('controller'=>'masters','action'=>'login'));
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end('Entrar');

模型

class Master extends AppModel{
public $name = 'Master';
public $validate = array(
    'username' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'Usuario requerido.'
        )
    ),
    'password' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'Senha requerida.'
        )
    )
);

public function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['password'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
    }
    return true;
}

我不知道为什么会出现这个错误..这一切似乎都好! 我改变了一封Security.salt的信,正如他问的那样。 帮我 :) 我需要它来工作

2 个答案:

答案 0 :(得分:1)

debug($this->Auth->login());
if ($this->Auth->login()) {}

是一个坏主意。 第一个会让你登录, 然后第二次调用 - 当然 - 返回false(因为你已经登录)。

如果你真的需要这样测试,请暂停代码:

debug($this->Auth->login()); die();

答案 1 :(得分:0)

我遇到了同样的问题,为我修复的是将$ this->别名更改为User,所以beforeSave()现在看起来像

public function beforeSave($options = array()) {
    if (isset($this->data['User']['password'])) {
        $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
    }
    return true;
}
相关问题