CakePHP 1.3:成功登录时收到authError消息

时间:2013-03-24 18:06:47

标签: cakephp cakephp-1.3

我正在尝试实施登录功能,用户可以使用其用户名或电子邮件地址。我已经用两者进行了登录,但是当用户使用他的电子邮件地址成功登录时,authError仍会闪烁(用户已登录)。我在登录操作中发表评论“HERE”,我不确定在重定向之后会发生什么。

以下是我的代码的相关部分:

App Controoler:

public $components = array(
    'Auth' => array(
        'authorize' => 'controller',
        'loginRedirect' => array(
            'controller' => 'users',
            'action' => 'welcome_page'
        ),
        'loginError' => 'Invalid user name/password',
        'authError' => 'You don\'t have permission'
    ),

    'Session',
);

用户控制器:

public function beforeFilter() {

    parent::beforeFilter();

    $this->Auth->allow('add');
}

public function login() {       

    // At this point, the Auth Component is unable to log in user, so check with email.
    if (!empty($this->data) &&
        !empty($this->Auth->data['User']['username']) &&
        !empty($this->Auth->data['User']['password'])) {        

        // Look for user with email address using the entered username
        $user = $this->User->find('first', array(
            'conditions' => array(
                'User.email' => $this->Auth->data['User']['username'],
                'User.password' => $this->Auth->data['User']['password']
            ),
            'recursive' => -1
        ));

        // Check if a matching user is found and that if login was succesfull
        if (!empty($user) && $this->Auth->login($user)) {       

            if ($this->Auth->autoRedirect) {
                // NOTE: user trying to log in with email reaches HERE
                $this->redirect($this->Auth->redirect());   // this is the default authentication redirect defined in App Controller
            }


        } else {
            $this->Session->setFlash($this->Auth->loginError, $this->Auth->flashElement, array(), 'auth');
        }
    }

}

1 个答案:

答案 0 :(得分:0)

我编辑了您的原始帖子,以删除会话变量中的消息。

<?php

$this->Session->delete('Message.flash');
$this->Session->delete('Message.auth');

?>

希望这有帮助!

-Andrew