Question

I have implemented a login function programmatically.

This code is following:

$token = new UsernamePasswordToken($user, $user->getPassword(), 'main', $user->getRoles());
$this->get('security.context')->setToken($token);

$event = new InteractiveLoginEvent($this->getRequest(),$token);
$this->get('event_dispatcher')->dispatch('security.interactive_login', $event);

After this, how do I implement remember me ?

(I know how to use form, but I want to implement remember me programmatically. )

please help...

Was it helpful?

Solution

Try to use RememberMeToken

$key = $this->getParameter('secret'); // your security key from parameters.yml

$token = new RememberMeToken($user, 'main', $key); 
$this->get('security.context')->setToken($token);

OTHER TIPS

I had the same problem.you should use RememberMeToken instead of UsernamePasswordToken.

The class parameters are very similar to UsernamePasswordToken so I don't go into details.

You can create a cookie with an unique ID. Then store this cookie in a database and check this database whenever a new visitor visits the site. In case you have your users stored in a database it would be best to make an extra column to store this information. Have a look at FOSUserBundle by the way.

I used the same method as forgottenbas suggested, but got an error. I had to use $this->container (Symfony2):

$key = $this->container->getParameter('secret');

$token = new RememberMeToken($user, 'user_area', $key); 
$this->get('security.context')->setToken($token);

$user is an instance of my User class and 'user_area' is the name of my firewall.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top