I am trying to implement rememberme functionality for my ZF2 v2.2 site. So here is what i have done so far : I created a service for session manager to write the session to db :
'session' => array(
'remember_me_seconds' => 2419200,
'use_cookies' => true,
'cookie_httponly' => true,
),
'session_manager' => function (ServiceManager $sm) {
$adapter = $sm->get('db_adapter');
$config = $sm->get('app_config');
$sessionOptions = new Session\SaveHandler\DbTableGatewayOptions();
$sessionTableGateway = new TableGateway('tbl_session', $adapter);
$saveHandler = new Session\SaveHandler\DbTableGateway($sessionTableGateway, $sessionOptions);
$sessionConfig = new Session\Config\SessionConfig();
$sessionConfig->setCookieDomain(ACTIVE_SITE);
$sessionConfig->setCookieSecure(true);
$sessionConfig->setOptions($config['session']);
$sessionManager = new Session\SessionManager($sessionConfig, NULL, $saveHandler);
$sessionManager->start();
return $sessionManager;
},
And used this session manager for my sessions and AuthenticationService
:
Session\Container::setDefaultManager($sm->get('session_manager'));
'user_auth_service' => function (ServiceManager $sm) {
$authService = new \Zend\Authentication\AuthenticationService();
$session = new \Zend\Authentication\Storage\Session(null, null, $sm->get('session_manager'));
$authService->setStorage($session);
return $authService;
},
And in my login form i use remember me :
public function login(\User\Model\User $user)
{
$authAdapter = $this->getServiceLocator()->get('user_auth_adapter');
$authAdapter->setIdentity($user->username);
$authAdapter->setCredential($user->password);
/* @var $authService \Zend\Authentication\AuthenticationService */
$authService = $this->getServiceLocator()->get('user_auth_service');
$result = $authService->authenticate($authAdapter);
switch ($result->getCode()) {
case \Zend\Authentication\Result::FAILURE_IDENTITY_NOT_FOUND:
case \Zend\Authentication\Result::FAILURE_CREDENTIAL_INVALID:
return $result->getMessages();
break;
case \Zend\Authentication\Result::SUCCESS:
$user = $authAdapter->getResultRowObject(null, 'password');
$user->rolls = $this->getServiceLocator()->get('user_role_table')->getRoles($user->id);
$authService->getStorage()->write($user);
getSM()->get('session_manager')->rememberMe();
return true;
break;
default:
return 'Invalid Credential Provided !';
break;
}
}
But the app still doesn't remember me .What am i doing wrong here ???