views:

103

answers:

2

I'm having 2 login forms in my (cake) application. One on the home page (served by pages controller) and one in my user controller. The one from my user controller is working fine. But when I try to login from the homepage I get a blank page and I see in firebug I got a 404. The strange thing is that the session is setup OK.

It's looks like it has something to do with $this->Auth->autoRedirect = false (which is set in user controller beforeFilter()). What could be the problem?

This is how my login action looks like:

  function login()
  {
    /* Werkt nog niet vanuit `home login` */
    if ($this->Auth->user())
    {
      if(!empty($this->data))
      {
        /* Update last login */
        $this->User->id = $this->Auth->user('id');
        $this->User->saveField('last_login', date('Y-m-d H:i:s'));
      }
      $this->redirect($this->Auth->redirect());
    }
  }
A: 

When you set $this->Auth->autoRedirect = false you need to manually redirect in your login() method after login attempt, f.ex. $this->redirect($this->referer());. I guess, you've got that blank page because of no redirect or wrong redirect. What is the url of that blank page?

bancer
The url of the blank page is /site/users/login, but inside my action I have a redirect like the one above.The strange thing however is that everything I put in login() is not executed (when requested from outside the user controller). Not even when I put it in the begin...
tersmitten
A: 

After trying everything I could think of, my last hope was not using the (default) pages controller. So what I did was copy it to my app/controllers and made it look like:

<?php
class PagesController extends AppController
{
  var $name = 'Pages';
  var $uses = array();

  var $__allowUnAuthorized = array('*');
  var $__allowAuthorized = array();

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

    $this->Auth->allow($this->__allowUnAuthorized);
  }

  function display()
  {
    // Same as default
  }
}

I also moved some stuff from my user_controller to my app_controller:

<?php
class AppController extends Controller
{
  var $name = 'App';
  var $components = array('Auth', 'Security', 'Session');
  var $helpers = array('Html', 'Form', 'Session');

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

    /* Auth */
    $this->Auth->autoRedirect = false;

    $this->Auth->loginAction = array(
      'controller' => 'users',
      'action' => 'login'
    );
    $this->Auth->loginRedirect = array(
      'controller' => 'users',
      'action' => 'index'
    );
    $this->Auth->loginError = __(
      'Ongeldige conbinatie van gebruikersnaam en wachtwoord', true
    );
    $this->Auth->authError =  __(
      'U bent niet bevoegd om deze pagina te bekijken' ,true
    );

    $this->Session->delete('Auth.redirect');
  }
}
?>

And now everything works fine! Maybe it had something to with the pages controller not having access to $this->Session or $this->Auth?

tersmitten