CakePHP Auth Component Not logging in when using $

2019-07-12 12:45发布

I'm new to cakePHP and I have read their documentation and I'm following their simple authentication example. I've also searched far and wide (including answers on this site) for an answer to my problem.

I'm using cakePHP 2.0 and my UsersController's login function looks like this:

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

My AppController looks like this:

public $components = array(
                    'Session',
                    'Auth' => array(
                            'loginRedirect' => array('controller' => 'pages', 'action' => 'display'),
                            'logoutRedirect' => array('controller' => 'pages', 'action' => 'display')
                    )
        );

public function beforeFilter() {
    $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
}

My User Model:

class User extends AppModel {

public $name = "User";

}

And the login form:

echo $this->Session->flash('auth');
echo $this->Form->create('User', array('action' => 'login'));
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end('Login');

The problem is it doesn't log a user in. When $this->Auth->login() is called without $this->request->data as a parameter nothing is written in the session and the page just refreshes.However when you supply the $this->request->data it works, credentials are written to the session and I'm redirected correctly. However I've noticed that you can 'log in' anyone with $this->Auth->login($this->request->data) even if they are not present in the database. In addition to this, if I enter wrong credentials nothing happens, no flash messages are displayed.

I'm not trying to do anything special, all I want is the ability to log a user in. I have a simple users table with username and password fields and I have followed their example and read their documentation, but still nothing I have tried has worked.

I apologise if I have not supplied all the information needed to provide a response. Any help is greatly appreciated.

2条回答
爷、活的狠高调
2楼-- · 2019-07-12 13:17

I had the exact same experience. What happened to me was, at one point, I was able to log in successfully using some username and password, but I never had any code in the view that showed me I was logged in. I was also really confused that when I subsequently logged in with random usernames and passwords, it still gave me a successful login notification.

I think what happened was, if you are in a logged-in state, then $this->Auth->login() will return true no matter what username and password you enter. The solution would probably be to somehow call $this->Auth->logout() in your program, or empty your cookies so that the program doesn't specify you as being logged in anymore. Then $this->Auth->login() will work as normal again.

查看更多
可以哭但决不认输i
3楼-- · 2019-07-12 13:33

where did you define the engine to use? probably the same problem as http://ask.cakephp.org/questions/view/unable_to_login which has been solved setting the engine/adapter as described in the documentation.

查看更多
登录 后发表回答