Symfony How to force Remember Me with Login Link?

126 views Asked by At

I'm working on a project app where i've added the possibility for a team leader to add members in his team. Then i added a link which sends a login link for the members of the team.

I would like to force the "Remember Me" specifically for the members of the team. they would have to login just once using the link in the mail, and then they could close the browser and go back later and they would still be logged in.

Here is my security.yaml

login_link:
    check_route: login_check
    signature_properties: [ 'id' ]
    lifetime: 900 #15minutes

And here is the function in the controller

/**
 * @Route("/magic", name="app_magic")
 */
public function magic(UserRepository $userRepository, LoginLinkHandlerInterface $loginLinkHandler, MailerInterface $mailer): Response
{
    $users = $userRepository->findBy(['chefEquipe' => $this->getUser()]);

    foreach ($users as $user) {
        $loginLinkDetails = $loginLinkHandler->createLoginLink($user);
        $email = (new Email())
            ->from('[email protected]')
            ->to($user->getEmail())
            ->subject('Magic login link')
            ->text('You can use this link to login: ' . $loginLinkDetails->getUrl());

        $mailer->send($email);
    }

    $this->addFlash('message', 'Un mail de connexion a été envoyé aux membres de votre équipe');
    return $this->redirectToRoute('app_test_registration_team');

}

How could i do that without activating always remember me so that it does not affect the 'admin' user ? Thanks a lot

1

There are 1 answers

1
sha On

You should enable always_remember_me in configs:

firewalls:
    ...
    main:
        ...
        remember_me:
            ...
            always_remember_me: true