0
votes

After migrating a fully functional Cake 1.3 application to the recently released 2.0 version Authentication has ceased to work.

I've changed the calling of the AuthComponent and the structure of the login action according to the updated 2.0 manual, to no avail. The strange thing is the user is actually validated by $this->Auth->login() as it reaches the part of the login function where the user is redirect to the url set by $this->Auth->redirect(). After that redirect however, $this->Auth->user() returns empty (as well as AuthComponent::user()) and the user isn't logged in by the Auth component.

Cake doesn't throw any error during the process, the flash messages for 'auth' remain empty.

Users are stored in a simple database table containing id, username, email, password and timestamp columns. The passwords are hashed and I've added some users using the new Cake 2.0 methods.

This is the code of AppController.php:

<?php
  class AppController extends Controller {
    public $helpers = array('Session', 'Html', 'Time', 'Form', 'Text');
    public $components = array('Session', 'RequestHandler', 'Auth');    

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

UserController.php:

<?php
  class UsersController extends AppController {
    public $name = 'Users';

    function beforeFilter() {
      parent::beforeFilter();
    }

    function login() {
      if ($this->request->is('post')) {
        if ($this->Auth->login()) {
          return $this->redirect($this->Auth->redirect());
        }
      }
    }

    function logout() {
      $this->redirect($this->Auth->logout());
    }
  }
?>

User.php model. I've disabled form validation for the time being after I solve this problem:

<?php
  class User extends AppModel {
    public $name = 'User';
  }
?>

The login view:

  <?php
    echo $this->Form->create('User');
    echo $this->Form->input('username', array('label' => 'Username', 'before' => '<p class="input" id="username">', 'after' => '</p>', 'between' => '<br />', 'div' => false));
    echo $this->Form->input('password', array('label' => 'Password', 'before' => '<p class="input" id="password">', 'after' => '</p>', 'between' => '<br />', 'div' => false));
    echo $this->Form->end('Login');
  ?>

I also tried to setting some of the Auth features in the $components variable in the AppController, which didn't work as well:

  public $components = array(
    'Auth'=> array(
      'loginAction' => array(
        'controller' => 'users',
        'action' => 'login',
      ),
      'loginRedirect' => array(
        'controller' => 'maps',
        'action' => 'index',
      ),
      'authenticate' => array(
        'Form' => array(
          'fields' => array('username', 'password')
        )
      )
    )
  );

What's causing the problem here? Routing maybe? I've commented out all routes except:

require CAKE . 'Config' . DS . 'routes.php';

UPDATE: After adding some debug statements in the login method in the UsersController I now know $this->Auth->user() is actually populated with the correct user after the call to $this->Auth->login(). After the redirect to another controller the login session is lost completely, however. So I still don't know what's going wrong here.

UPDATE 2 I've restarted the process of migrating by taking my working 1.3 application and running the migration console script on it like I did the last time.

This time I noticed the script stopped because of two errors relating to custom components. Component classes should extend Component now, instead of the 1.3 default: Object.

After fixing these component errors I ran the migration script again (something I neglected to do during the first migration attempt) and implemented the new AuthCompenent call. So far everything seems to be working correctly. Not sure what's different now and what went wrong the first time, as Cake didn't output any error messages.

UPDATE 3 It's getting weirder. I thought I solved it, but after transferring my code to another development machine Auth suddenly stops working. It's working on my main setup, but while testing on another it fails again following the same scenario. I've cleared the cache to be sure, but it still isn't working. Cake doesn't generate any error output.

UPDATE 4 It appears to be a Session problem on my machine. I've just set the Session to be stored in a cookie and suddenly Auth starts working again. Not sure why the default Session isn't working and I don't know where to start debugging in that case.

Only cookie sessions appear to work, defining a database session has the same result as a regular session; Auth stops working.

2

2 Answers

1
votes

Try it with use_trans_sid enabled in /Config/core.php:

Configure::write('Session', array(
    //'defaults' => 'php'
    'defaults' => 'cake',
    'cookie' => 'CAKEPHP2',
    'ini' => array('session.use_trans_sid' => true)
));
0
votes

Did you try also to configure the Authentication handler ?

public $components = array(
    'Auth'=> array(
        'authenticate' => array('Form')
    )
);