title: Create a symfony2 remember me cookie manually (FOSUserBundle)
body: <p>Could somebody explain how you can manually create a remember me cookie in a controller?</p>
  
  <p>I want the users to stay logged in after they pressed the "register"
  button, without having to login with their credentials afterwards.</p>
  
  <p>I've tried to create a cookie manually but i'm guessing the cookie
  value is incorrect, and therefor the "remember me" functionality
  doesn't work.
  A cookie with the correct name gets set. I've checked that.</p>
  
  <p>The remember me functionality works as expected when using the normal
  login procedure with the user's credentials.</p>
  
  <p>security.yml
  security.yml remember me</p>
  
  <pre><code>security:
     firewalls:
         main:
             remember_me:
                 lifetime: 86400
                 domain:   ~
                 path:     /
                 key:      myKey
  </code></pre>
  
  <p>This is what I have now, even though the cookie is set, it doesn't work. </p>
  
  <pre><code>$um = $this-&gt;get('fos_user.user_manager');
  $member = $um-&gt;createUser();
  
  … Form stuff with bindRequest etc.
  
  $um-&gt;updatePassword($member);
  $um-&gt;updateUser($member);
  
  $providerKey = $this-&gt;container-&gt;getParameter('fos_user.firewall_name');
  $securityKey = 'myKey';
  
  $token = new RememberMeToken($member, $providerKey, $securityKey,
  $member-&gt;getRoles());
  $this-&gt;container-&gt;get('security.context')-&gt;setToken($token);
  
  $redirectResponse = new RedirectResponse($url);
  $redirectResponse-&gt;headers-&gt;setCookie(
     new \Symfony\Component\HttpFoundation\Cookie(
         'REMEMBERME',
         base64_encode(implode(':', array($member-&gt;getUsername(),
  $member-&gt;getPassword()))),
         time() + 60*60*24
     )
  );
  return $redirectResponse;
  </code></pre>
  
  <p><strong>Update:</strong></p>
  
  <p>I've also tried working with the
  PersistentTokenBasedRememberMeServices class with reflection but it does not work. a cookie gets set but it's not working</p>
  
  <pre><code>$token = $this-&gt;container-&gt;get('security.context')-&gt;getToken();
  
  $providerKey = $this-&gt;container-&gt;getParameter('fos_user.firewall_name');
  $securityKey = 'myKey';
  
  $persistenService = new
  PersistentTokenBasedRememberMeServices(array($um), $providerKey,
  $securityKey, array('path' =&gt; '/', 'name' =&gt; 'REMEMBERME', 'domain' =&gt;
  null, 'secure' =&gt; false, 'httponly' =&gt; true,
  'lifetime' =&gt; 86400));
  $persistenService-&gt;setTokenProvider(new InMemoryTokenProvider());
  
  $method = new \ReflectionMethod('Symfony\Component\Security\Http\RememberMe\PersistentTokenBasedRememberMeServices',
  'onLoginSuccess');
   $method-&gt;setAccessible(true);
  $method-&gt;invoke($persistenService, $request, $redirectResponse, $token);
  </code></pre>
  
  <p>I'm using Symfony v2.0.5 and FOSUserBundle 1.0</p>
  
  <p>UPDATE 2:</p>
  
  <p>I've tried a 3rd way. The same as above but without reflection:</p>
  
  <pre><code>$token = $this-&gt;container-&gt;get('security.context')-&gt;getToken();
  
  $providerKey = $this-&gt;container-&gt;getParameter('fos_user.firewall_name');
  $securityKey = 'myKey';
  
  $persistenService = new PersistentTokenBasedRememberMeServices(array($um), $providerKey, $securityKey, array('path' =&gt; '/', 'name' =&gt; 'REMEMBERME', 'domain' =&gt; null, 'secure' =&gt; false, 'httponly' =&gt; true, 'lifetime' =&gt; 31536000, 'always_remember_me' =&gt; true, 'remember_me_parameter' =&gt; '_remember_me'));
  $persistenService-&gt;setTokenProvider(new InMemoryTokenProvider());
  
  $persistenService-&gt;loginSuccess($request, $redirectResponse, $token);
  </code></pre>