0

我正在尝试实现一个登录功能,用户可以使用他的用户名或电子邮件地址。我已经解决了两者的登录问题,但是当用户使用他的电子邮件地址成功登录时,authError 仍然闪烁(用户已登录)。我在登录操作中添加了一条评论“这里”,但我不确定重定向之后会发生什么。

以下是我的代码的相关位:

应用程序控制器:

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

}
4

1 回答 1

0

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

<?php

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

?>

希望这可以帮助!

-安德鲁

于 2013-03-26T02:44:49.283 回答