public function loginAction() { $userLogin = new AuthenticationService(); if ($userLogin->hasIdentity()) { $identity = $userLogin->getIdentity(); return $this->forward()->dispatch('Application\\Controller\\Index', array('action' => 'listar')); } else { $form = new Login(); $form->get('submit')->setValue('Login'); $messages = null; $request = $this->getRequest(); if ($request->isPost()) { $loginFilters = new LoginValidator(); $form->setInputFilter($loginFilters->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $data = $form->getData(); $sm = $this->getServiceLocator(); $dbAdapter = $sm->get('Zend\\Db\\Adapter\\Adapter'); $config = $this->getServiceLocator()->get('Config'); $indexAdapter = new IndexAdapter($dbAdapter, 'zml_admin', 'usr_name', 'usr_password'); $indexAdapter->setIdentity($data['usr_name'])->setCredential($data['usr_password']); $auth = new AuthenticationService(); // or prepare in the globa.config.php and get it from there. Better to be in a module, so we can replace in another module. //$auth = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService'); // $sm->setService('Zend\Authentication\AuthenticationService', $auth); // You can set the service here but will be loaded only if this action called. $result = $auth->authenticate($indexAdapter); switch ($result->getCode()) { case Result::FAILURE_IDENTITY_NOT_FOUND: echo "prueba"; die; break; case Result::FAILURE_CREDENTIAL_INVALID: echo "prueba"; die; break; case Result::SUCCESS: $storage = $auth->getStorage(); $storage->write($indexAdapter->getResultRowObject(null, 'usr_password')); $time = 1209600; // 14 days 1209600/3600 = 336 hours => 336/24 = 14 days // if ($data['rememberme']) $storage->getSession()->getManager()->rememberMe($time); // no way to get the session if ($data['rememberme']) { $sessionManager = new \Zend\Session\SessionManager(); $sessionManager->rememberMe($time); } return $this->forward()->dispatch('Application\\Controller\\Index', array('action' => 'listar')); break; default: echo "prueba"; die; break; } foreach ($result->getMessages() as $message) { $messages .= "{$message}\n"; } } } $viewModel = new ViewModel(array('form' => $form, 'messages' => $messages)); $viewModel->setTerminal(true); return $viewModel; } }