Search code examples
sessioncakephppage-refresh

CakePHP Session, Not displaying on first refresh


I am using CakePHP 1.3 latest version for my Administrator / User login.

I am facing a very weird issue, whenever I am trying to login using Auth, on first attempt it is not displaying me any information into SESSION but whenever I refresh the page again, all information is coming to Session.

Any ideas, why this happends?

Code is here from app_controller.php file

function beforeFilter()
{   
    $this->Auth->autoRedirect = false;
    $this->Auth->fields = array('username' => 'email_address', 'password' => 'password');
    $this->Auth->loginAction = array('admin' => false, 'controller' => 'users', 'action' => 'login');
    $this->Auth->userScope = array('User.status' => '1','User.paymentstatus' => '0');
    if($this->Auth->User())
    {
        if($this->Session->read('Auth.User.user_type') == 3) {
            $this->layout = 'admin';
            $this->Auth->loginRedirect = array('controller' => 'admins', 'action' => 'welcome');
        }
        else 
        {
            $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'display', 'home');
        }
    }
}

function beforeRender(){
    $this->set('auth', $this->Auth->user());
    header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); // // HTTP/1.1
    header("Pragma: no-cache");
    header("Expires: Mon, 17 Dec 2007 00:00:00 GMT"); // Date in the past
}

Earliest response would be appreciated.

Thanks !


Solution

  • function beforeFilter(){   
      parent::beforeFilter();
      $this->Auth->autoRedirect = false;
      $this->Auth->fields = array('username' => 'email_address', 'password' => 'password');
      $this->Auth->loginAction = array('admin' => false, 'controller' => 'users', 'action' => 'login');
      $this->Auth->userScope = array('User.status' => '1','User.paymentstatus' => '0');
      if($this->Auth->user() && $this->Auth->user('user_type') == 3)
            $this->layout = 'admin';
    }
    function login(){
      if($this->Auth->user()){
        if($this->Auth->user('user_type') == 3) {
            $this->redirect(array('controller' => 'admins', 'action' => 'welcome'));
        } else  {
            $this->redirect(array('controller' => 'pages', 'action' => 'display', 'home'));
        }
      }
     }
    

    You don't have to set auth in beforeRender, you can still access that in the view with $this->Session->read('Auth.User');