Skip to content

Programatically login and RememberMe #50909

@clustermin

Description

@clustermin

Description

Before Symfony 6.2, I could programmatically login with RememberMe enabled like this:

    #[Route('/register', name: 'app_register', methods: ['GET', 'POST'])]
    public function register(
        UserRepository             $userRepository,
        Request                    $request,
        LoginFormAuthenticator     $authenticator,
        UserAuthenticatorInterface $userAuthenticator,
    ): Response
    {

        //...
        return $userAuthenticator->authenticateUser($user, $authenticator, $request, [(new RememberMeBadge())->enable()]);
    }

Symfony 6.2 added a cleaner way to programmatically login, but it doesn't support badges.

$security->login($user, LoginFormAuthenticator::class, 'main');

I'd like to suggest adding an optional parameter to $security->login() to support badges.

Example

    // symfony/security-bundle/Security.php

    public function login(UserInterface $user, string $authenticatorName = null, string $firewallName = null, array $badges = []): ?Response
    {
        // ...
        return $this->container->get('security.authenticator.managers_locator')->get($firewallName)->authenticateUser($user, $authenticator, $request, $badges);
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions