W
W
Webspec2021-01-24 20:02:11
symfony
Webspec, 2021-01-24 20:02:11

How to authorize a user immediately after registration?

Hello! Please tell me about the automatic login of the user immediately after registration.

Latest version of Symfony 5.2
I do as indicated in some examples on the Internet:

GuardAuthenticatorHandler $guardHandler,
LoginFormAuthenticator $formAuthenticator

And call:
$guardHandler->authenticateUserAndHandleSuccess(
$user,
$request,
$formAuthenticator,
'main'
);

The IDE and the code itself complain that $formAuthenticator does not inherit the \Symfony\Component\Security\Guard\AuthenticatorInterface interface.
What should be passed there then?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Webspec, 2021-01-26
@Webspec

The answer turned out to be simple.
If you are using the new authorization system, then so be it.

/**
     * Тест системы авторизации
     *
     * @Route("/test/", name="test")
     *
     * @param Request $request
     * @param LoginFormAuthenticator $loginFormAuthenticator
     * @param UserAuthenticatorInterface $userAuthenticator
     *
     * @return Response
     * @throws ServiceException
     */
    public function test(
        Request $request,
        LoginFormAuthenticator $loginFormAuthenticator,
        UserAuthenticatorInterface $userAuthenticator
    ): Response
    {
        $user = $this->userService->getUserByEmail('[email protected]');
        return $userAuthenticator->authenticateUser($user, $loginFormAuthenticator, $request);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question