Can someone explain how you can manually create a cookie to remember me in the controller?
I want users to remain on the system after they click the "register" button, without having to log in with their credentials.
I tried to create a cookie manually, but I assume that the cookie value is incorrect, and therefore the โremember meโ function does not work. A cookie with the correct name is set. I checked it out.
Remember that functionality works as expected; when using normal, log in with user credentials.
security.yml security.yml remember me
security: firewalls: main: remember_me: lifetime: 86400 domain: ~ path: / key: myKey
This is what I have now, even if the cookie is set, it does not work.
$um = $this->get('fos_user.user_manager'); $member = $um->createUser(); โฆ Form stuff with bindRequest etc. $um->updatePassword($member); $um->updateUser($member); $providerKey = $this->container->getParameter('fos_user.firewall_name'); $securityKey = 'myKey'; $token = new RememberMeToken($member, $providerKey, $securityKey, $member->getRoles()); $this->container->get('security.context')->setToken($token); $redirectResponse = new RedirectResponse($url); $redirectResponse->headers->setCookie( new \Symfony\Component\HttpFoundation\Cookie( 'REMEMBERME', base64_encode(implode(':', array($member->getUsername(), $member->getPassword()))), time() + 60*60*24 ) ); return $redirectResponse;
Update:
I also tried working with the PersistentTokenBasedRememberMeServices class with reflection, but it does not work. cookie is set, but it does not work
$token = $this->container->get('security.context')->getToken(); $providerKey = $this->container->getParameter('fos_user.firewall_name'); $securityKey = 'myKey'; $persistenService = new PersistentTokenBasedRememberMeServices(array($um), $providerKey, $securityKey, array('path' => '/', 'name' => 'REMEMBERME', 'domain' => null, 'secure' => false, 'httponly' => true, 'lifetime' => 86400)); $persistenService->setTokenProvider(new InMemoryTokenProvider()); $method = new \ReflectionMethod('Symfony\Component\Security\Http\RememberMe\PersistentTokenBasedRememberMeServices', 'onLoginSuccess'); $method->setAccessible(true); $method->invoke($persistenService, $request, $redirectResponse, $token);
I am using Symfony v2.0.5 and FOSUserBundle 1.0
UPDATE 2:
I tried the third way. Same as above, but without reflection:
$token = $this->container->get('security.context')->getToken(); $providerKey = $this->container->getParameter('fos_user.firewall_name'); $securityKey = 'myKey'; $persistenService = new PersistentTokenBasedRememberMeServices(array($um), $providerKey, $securityKey, array('path' => '/', 'name' => 'REMEMBERME', 'domain' => null, 'secure' => false, 'httponly' => true, 'lifetime' => 31536000, 'always_remember_me' => true, 'remember_me_parameter' => '_remember_me')); $persistenService->setTokenProvider(new InMemoryTokenProvider()); $persistenService->loginSuccess($request, $redirectResponse, $token);