I am trying to automatically login in a programatical way. (context; I got a seperate account system for admins, if a person is an admin (s)he should be able to login as a user, so I got the userIdentifier)
In symfony 3.3 the way this was done was by;
$securityContext = $this->get('security.token_storage');
$token = new UsernamePasswordToken($user, $user->getPassword(), 'main'], $user->getRoles());
$securityContext->setToken($token);
In symfony 5.* I did read something about UserAuthenticatorInterface::authenticateUser(UserInterface $user)
However, in Symfony 6 it requires more arguments, one of them being a AuthenticatorInterface and a Request (public function authenticateUser(UserInterface $user, AuthenticatorInterface $authenticator, Request $request, array $badges = []): ?Response;). I get that it is used when a user requests authentication with a request that these could be used in this function.
I already tried it by making a UsernamePasswordToken and adding that token to the storage (TokenStorageInterface $tokenStorage);
$token = new UsernamePasswordToken($user, 'main', $user->getRoles());
$tokenStorage->setToken($token);
But then I got;
You cannot refresh a user from the EntityUserProvider that does not contain an identifier. The user object has to be serialized with its own identifier mapped by Doctrine.
What is the best way to login an user programatically?