4

I'm trying to use authentication and sessions in a ZF2 App. So far I have the follow code:

In my Module.php:

// (...) rest of code

public function getServiceConfig()
    {
        return array(
                'factories' => array(
                        // (...) Other factories

                        // Authentication Service
                        'AuthService' => function($sm) {
                            $dbAdapter           = $sm->get('Zend\Db\Adapter\Adapter');
                            $dbTableAuthAdapter  = new DbTable($dbAdapter,
                                'sec_user','login','password');

                            $authService = new AuthenticationService();
                            $authService->setAdapter($dbTableAuthAdapter);

                            return $authService;
                        },
                ),
        );
    }

// (...) rest of code

Then in my Controller Login action I have:

use Zend\Session\Container;

// (...) rest of code

    public function loginAction()
    {
       $this->getAuthService()->getAdapter()
                  ->setIdentity('testlogin')
                  ->setCredential('testpass');

        $auth_result = $this->getAuthService()->getAdapter()->authenticate();

        if ($auth_result->isValid()) {
            $session = new Container(); // Exception line
            $session->login = 'testlogin';

            // (...) other code
        }
    }
// (...) rest of code

The code works fine, validate the credentials successfully but when I try to save some value to SESSION with the simplest example I found in documentation, it fails. The line:

$session = new Container();

Is triggering this Exception:

Session validation failed

What else can I test? Any idea about what I'm doing wrong will be appreciated.

Bellow is the full trace:

File:
/var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/Session/SessionManager.php:111


Message:

Session validation failed


Stack trace:

#0 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/Session/AbstractContainer.php(78): Zend\Session\SessionManager->start()
#1 /var/www/sismedical/Server/module/Security/src/Security/Controller/AuthController.php(100): Zend\Session\AbstractContainer->__construct()
#2 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractActionController.php(83): Security\Controller\AuthController->loginAction()
#3 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#4 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#5 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#6 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#7 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/Mvc/DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#8 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#9 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#10 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#11 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(309): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
3
  • 1
    Do you by any chance have an event listener listening to all events triggered by all classes? (ie $sharedEvents->attach('*', '*', .. ) Shot in the dark, but I know for sure that such a listener will result in the above error. Commented Jul 2, 2013 at 15:21
  • @Crisp I haven't enough words to thank you :) please add the Answer to accept it. Commented Jul 2, 2013 at 15:41
  • Glad I could be of help, it was a real pain to debug when it happened to me :o[. Answer added ;) Commented Jul 2, 2013 at 15:51

1 Answer 1

5

Attaching an event listener to listen to all events triggered by all classes $sharedEvents->attach('*', '*', .. ) will cause this error message.

You have to be really careful what you attach listeners to, since some listeners are expecting a specific response (in this case a boolean indicating if session is valid). A listener that wasn't designed to handled the event, such as a log listener, will typically not return a response, which in this case is seen as a validation failure.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.