Search code examples
zend-frameworkframeworksidentityzend-auth

Zend_Auth Identity not persisting between controllers


Fairly new to Zend Framework 1.11 with Doctrine 2.

I have successfully created a login etc using Doctrine.

My current problem though is that the Zend_Auth instance works fine when I stay within the controller that the log in is within.

If I try determine the state of Zend_Auth::getInstance->HasIdentity() in any other controller it returns blank.

If I then go back to any page residing within the controller containing the login/authentication hasIdentity works fine.

I have even tried writing to the storage but this provides no joy.

My auth code is as follows which is the action called after clicking Login (Within MembersareaController)

public function authAction()
{
    $this->_helper->viewRenderer->setNoRender(true);

    $loginForm = new Application_Model_Login();

    if($this->getRequest()->isPost()){
        $usr = "";
        $pwd = "";
        $KeepLoggedIn = false;
        $message = "";

        $usr = $this->_getParam('username'); 
        $pwd = $this->_getParam('password');
        $pwdMd5 = md5($pwd);

        if($usr !== "" && $pwd !== ""){

            $GDSAdaptor = new ZC_Auth_Adapter($usr, $pwdMd5);

            $result = \Zend_Auth::getInstance()->authenticate($GDSAdaptor);

            if(\Zend_Auth::getInstance()->hasIdentity()){
                $this->flashMessenger->addMessage(LOGIN_SUCCESS);
                $this->_redirect('/membersarea/index');
            }else{
                $this->flashMessenger->addMessage(LOGIN_INVALID);
            }
        }else{
            $this->flashMessenger->addMessage(LOGIN_MISSING_FORMVAL);
            $this->_redirect('/membersarea/login');
        }
    }

Trying to seeing a person is logged in on the IndexController with the following code produces no results. hasIdentity returns a blank value.

public function indexAction()
{

    if(Zend_Auth::getInstance()->hasIdentity())
    {
        $msg = "hasIdentity: YES";
    }else{
        $msg = "hasIdentity: NO";
    }

    $this->view->msg = $msg;
}

Solution

  • Zend_Session::rememberMe(); in the bootstrap solved this issue