0
votes

Sorry to bring this topic up again, but I've searched all the answers I can on this topic, but have not found a solution(I'm very new to cakephp): I use the password routine to hash my password in my AppController I have:

class AppController extends Controller {
    public $components = array('DebugKit.Toolbar','Session','Auth');    
}

in my UsersController I have:

public function add() {
    if ($this->request->is('post')) {
        $this->User->create();
        // hash the password coming in from the form using Authcomponent::password
        $this->request->data['User']['password'] = AuthComponent::password($this->request->data['User']['password']);           
        if ($this->User->save($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }
}

/** login method */
    public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                //redirect to page he was trying to access before login
                return $this->redirect($this->Auth->redirectUrl());
            } else {
                $this->Session->setflash('Invalid username or password');
            }
        }
    }

The issue is that I cannot log back in after adding a user: I get the setflash message. The password is being hashed correctly on the MySQL database.

Any help appreciated: I'm at a loss how to debug this.

EDIT I've tried other solutions, from the cakephp site (no success) and 2 youtube sites (no success). I have also tried plain passwords and hashed passwords (using the default and blowfish) all with the same result.

I have added the debug statements to the code as follows:

public function login() {
     pr($this->request->data);      //debug
    if ($this->request->is('post')) {   //devbug
    echo ('post request');}             //debug
    if ($this->request->is('post')) {
debug($this->Auth->login());  //debug
debug($this->request->data);  //debug
            if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Session->setFlash(__('Invalid username or password, try again'));
    }
}

The array displayed using pr($this->request->data); shows the correct data, however when I use debug($this->request->data); it shows only 5 characters in the password. Could t his be the issue (or a red herring?) result as displayed follows:

Array
(
    [User] => Array
        (
            [username] => user
            [password] => password
        )

)

post request
\app\Controller\UsersController.php (line 18)

false

\app\Controller\UsersController.php (line 19)

array(
    'User' => array(
        'password' => '*****',
        'username' => 'user'
    )
)
3

3 Answers

0
votes

You should try this

AppController

class AppController extends Controller {

public $components = array(
    'RequestHandler','Session',
    'Auth' => array(
        'Autoredirect'=>false,
        'loginRedirect' => array('controller' => 'users', 'action' => 'user_dashboard'),
        'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
        'authError' => 'Did you really think you are allowed to see that?',
    )
);

UsersController

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
             return $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setflash('Invalid username or password');
        }
    }
}
0
votes

Try adding this line in the login function:

public function login() {
  pr($this->request->data);//LINE ADDED

if ($this->request->is('post')) {
    if ($this->Auth->login()) {
         return $this->redirect($this->Auth->redirect());
    } else {
        $this->Session->setflash('Invalid username or password');
    }
}

you will see what data you are passing to the form login.

0
votes

You are saving an encrypted password, but when you log in your software expects an unencrypted password. Try to put a password unencrypted to your database and it should work.

Try this here in your app controller:

public $components = array('DebugKit.Toolbar','Session','Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'passwordHasher' => 'Blowfish',
            ),
        )
));

If that is still not working, please post your login-form as well.