Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement remember me programatically in symfony2?

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...

like image 535
Yajap Avatar asked Aug 03 '13 12:08

Yajap


2 Answers

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);
like image 169
Alexey B. Avatar answered Nov 14 '22 01:11

Alexey B.


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.

like image 29
CoderInNetwork Avatar answered Nov 14 '22 01:11

CoderInNetwork