0

我是 cakephp 新手。登录时遇到问题。使用错误的名称和密码重定向到登录主页。

用户控制器.php

public function login() {
    $this->layout = 'admin-login';
    if ($this->request->is('post')) {
        if ($this->Auth->login($this->request->data)) {
            return $this->redirect($this->Auth->redirectUrl())
        } else {
            $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
        }
    }
}

应用控制器.php

 public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
        'loginAction' => array('controller' => 'users', 'action' => 'login')
    )
);

 public function beforeFilter() {
    $this->Auth->allow("login");
    //$this->Auth->authorize = array('Controller');
    $this->Auth->authenticate = array(
        'Form' => array (
            'scope'  => array(
                'User.is_active' => 1
            ) 
        )
    );   
}
public function isAuthorized($user) {
    return true;
}

登录.ctp

echo $this->Form->create('User');
echo $this->Form->input('username');
echo $this->Form->input('password'); 
echo $this->Form->submit(__('Submit');
echo $this->Form->end();

当我填写错误的用户名和密码并单击提交按钮时,它会重定向到主页,谢谢。

4

2 回答 2

3

你用AuthComponent::login()错了,你只应该把数据传递给它,以防你想手动登录用户,即没有自动身份验证。

如果您想使用组件身份验证功能,只需调用$this->Auth->login()

另请参阅:http ://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#identifying-users-and-logging-them-in

在 2.x$this->Auth->login($this->request->data)中将使用发布的任何数据登录用户,而在 1.3$this->Auth->login($this->data)中将首先尝试识别用户并且仅在成功时登录。

于 2013-11-14T06:07:20.567 回答
-1
usercontroller

public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('login','logout');
    }



public function login()
    {
       $this->layout= 'login';

       if ($this->request->is('post')) {

            if ($this->Auth->login()) {

                        $this->redirect('/users');
                        else {

                $this->Session->setFlash(__('Invalid email or password, please try again'));

            }
        }
        else{
            if($this->Auth->loggedIn())
                $this->redirect('index');
        }
    }
}

应用控制器

class AppController extends Controller {

public $components = array(

    'Session',
    'Auth' => array(
        'authenticate' => array(
            'Form' => array( 
                'userModel' => 'User',
                 'fields' => array(
                    'username' => 'user_name',
                    'password' => 'password'
                    )
                )
            ),
        'loginAction' => array('controller' => 'users', 'action' => 'login'),
                    'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'),
        'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
        'authError' => 'You don\'t have access here.',
                /*
                'loginAction' => array('controller' => 'users', 'action' => 'forgot_password'),
                'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'),
                'logoutRedirect' => array('controller' => 'users', 'action' => 'forgot_password'),
                'authError' => 'You don\'t have access here.',
                */
        ),

);
于 2013-11-14T06:16:49.443 回答