3
votes

The web app is working great except for auth manual calls. I've been struggling with this for days. In my sample code I have temporarily rewritten the cookie to narrow down the cause. Here is my app controller snip:

App::import('Sanitize');
//uses('sanitize');
class AppController extends Controller {
    var $components = array('Clean','Acl', 'Auth', 'Session', 'RequestHandler', 'Cookie', /* 'DebugKit.Toolbar' */);
    var $helpers = array('uiNav','Flash','Html', 'Form', 'Session','Javascript','Ajax','Js' => array('Jquery'), 'Time','Js');

    function beforeFilter() {
        //Configure AuthComponent
        $this->Auth->authorize = 'actions'; 
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
        $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'view');
        $this->Auth->actionPath = 'controllers/';
        $this->Auth->autoRedirect = false;

        $this->Auth->allowedActions = array('display');

        if(!$this->Auth->user()){
            //$cookie = $this->Cookie->read('Auth.User');
            $cookie = array('username' => 'chris22', 'password' => 'stuff');
            if (!is_null($cookie)) {
                $this->set('checking_cookie',$cookie);
                if ($this->Auth->login($cookie)) {
                    $this->set('cookie_message','cookie validates!');
                    //  Clear auth message, just in case we use it.
                    $this->Session->delete('Message.auth');
    /*              $this->redirect($this->Auth->redirect()); */
                }
            }
        }
    }
}

As you can see I'm just plugging the user name and password into $this->Auth->login and it's not working!!

I don't know if my user controller is relevent, but here is the login function for that too:

function login() {
    if ($this->Auth->user()) {
        if (!empty($this->data) && $this->data['User']['remember_me'] && isset($this->data['User']['password'])) {
            $cookie = array();
            $cookie['username'] = $this->data['User']['username'];
            $cookie['password'] = $this->data['User']['password'];
            $this->Cookie->write('Auth.User', $cookie, true, '+1 month');
            //unset($this->data['User']['remember_me']);
            $this->set('cookie-00', 'setting cookie.');

        }else{ $this->set('cookie_message', 'not setting cookie.');}

        $this->redirect($this->Auth->redirect());
    }

}    

Thanks!

2

2 Answers

6
votes

EDIT - 1 - I think I know why this is not working for you.

Reason 1: $this->Auth->login takes data in the form of

array(
    'User'=>array(
        'username'=>'myusername',
        'password'=>'mypassword'
    )
)

Reason 2: $this->Auth->login does NOT hash the password.

You must send the password exactly as it appears in the database.

EVERYTHING BELOW THIS LINE IS POSSIBLE BUT NOT LIKELY IN THIS CASE:

Are you sure that when you originally created the username and password, the hashes were setup?

  1. To check that your hashes match look at your users table with phpmyadmin or mysql workbench and find the password field for the user chris22
  2. Compare that entry to your current hashing. To check your current hash, put the code below somewhere in a controller function (index) and navigate there.

    debug(Security::hash('stuff'));
    exit;
    

I hope this helps!

1
votes

Make sure the fields are correctly assigned in the Auth component. if you use different field names than username & password to connect, you must declare them in your controller like this way :

'Auth' => array(
  'authenticate' => array(
    'Form' => array(
      'fields' => array('username' => 'email', 'password' => 'mot_de_passe')
    )
  )
)