How to implement remember me programmatically in symfony2?

I implemented the login function programmatically.

This code is as follows:

$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 that, how do I implement, remember me?

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

please, help...

+7
php login symfony remember-me
source share
4 answers

Try using RememberMeToken

 $key = $this->getParameter('secret'); // your security key from parameters.yml $token = new RememberMeToken($user, 'main', $key); $this->get('security.context')->setToken($token); 
+8
source share

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

The class parameters are very similar to UserPasswordToken, so I do not go into details.

+1
source share

You can create a cookie with a unique identifier. Then save this cookie in the database and check this database whenever a new visitor visits the site. If you have your users that are stored in a database, it would be better to make an extra column to store this information. Take a look at the FOSUserBundle, by the way.

0
source share

I used the same method as the forgotten database, 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.

0
source share

All Articles