简体   繁体   中英

Multiple login forms submitting to one login action

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());
    }
  }

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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM