use App\Entity\Passport;
use Symfony\Component\OptionsResolver\OptionsResolver;
// ...
class PassportType extends AbstractType
{
// ...
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Passport::class,
]);
}
}
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
class SecurityService
{
private $authenticationManager;
private $tokenStorage;
public function __construct(
AuthenticationManagerInterface $authenticationManager,
TokenStorageInterface $tokenStorage,
)
{
$this->authenticationManager = $authenticationManager;
$this->tokenStorage = $tokenStorage;
}
public function test()
{
$user = ...; // entity User
$unauthenticatedToken = new UsernamePasswordToken(
$user,
$user->getPlainPassword(),
'main'
);
$authenticatedToken = $this
->authenticationManager
->authenticate($unauthenticatedToken);
$this->tokenStorage->setToken($authenticatedToken);
}