1

In the login action I'm having the following code:

public function login($sEmail, $sEncryptedPassword, $bIsClear = true)
{
    $manager = $this->getServiceLocator()->get('session_manager');
    $manager->start();

    Container::setDefaultManager($manager);

    $this->auth = new AuthenticationService();
    $this->auth->setStorage(new Session('FSP'));

    $dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
    $this->authAdapter = new AuthAdapter(
        $dbAdapter,
        'fsp_user',
        'email',
        'password'
    );

   $this->authAdapter
        ->setIdentity($sEmail)
        ->setCredential($sEncryptedPassword);

   $authAuthenticate = $this->auth->authenticate($this->authAdapter);

   if ($authAuthenticate->isValid()) {


        $user = $this->authAdapter->getResultRowObject();



        $storage = $this->auth->getStorage();

        $storage->write(
            array(
                'email' => $user->email,
                'first_name' => $user->first_name,
                'last_name' => $user->last_name,
                'id' => $user->id
            )
        );
   }

I have two problems with this code: 1) I'm saving the session in the database, and the session SaveHandler is configured in a service manager. I don't know if once I'm using Zend\Authenticate I should use the session manager too. In the documentation is saying that

"Unless specified otherwise, Zend\Authentication\AuthenticationService uses a storage class named Zend\Authentication\Storage\Session, which, in turn, uses Zend\Session."

So my first question is: can I configure the sessionHandler using just Zend\Authenticate or do I have to use the session manager?

2)I can't figured out how session storage is working in ZF. After login, the session data is not persisted in the DB. If I'm doing some debugging I get the following data:

    $session = new Container("FSP");
    //this returns the session data 
    var_dump($session->getIterator());


  //this returns empty
    var_dump($this->auth->getStorage());


  //this returns null, but I do have a FSP named cookie with an Id, showing in Chrome's developer tool
    $cookie = $this->getServiceLocator()->get('request')->getHeaders()->get('cookie');
    $sessionId = $cookie->FSP;
    var_dump($sessionId);

However, if I'm doing a refresh on the login (the login action is run again) the data from the previous session is wrote in the DB and not the data from the current one. So the second question is, why the session data is not persisted in the database at login and at what step in the session instantiation process is the cookie with the session ID created?

sica07
  • 4,896
  • 8
  • 35
  • 54

0 Answers0