I'm trying to create a token on registration.
I use FosUserBundle
as authenticator and FOSOAuthServerBundle
for API.
Everything works well when i generate a token manually. But i want to generate it automatically after a successful registration. So i created a EventListener
called TokenSetter
Here is the code
class TokenSetter
{
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function postPersist(LifecycleEventArgs $args)
{
$user = $args->getEntity();
if ($user instanceof Account) {
$clientManager = $this->container->get('fos_oauth_server.client_manager.default');
$client = $clientManager->createClient();
$client->setRedirectUris(array('http://127.0.0.1:8000'));
$client->setAllowedGrantTypes(array('password', 'refresh_token'));
$clientManager->updateClient($client);
$grantRequest = new Request(array(
'client_id' => $client->getPublicId(),
'client_secret' => $client->getSecret(),
'grant_type' => 'password',
'username' => $user->getUsername(),
'password' => $user->getPlainPassword()
));
$tokenResponse = $this->container->get('fos_oauth_server.server')->grantAccessToken($grantRequest);
$token = $tokenResponse->getContent();
}
}
}
The issue is that the $user->getPlainPassword()
returns a empty value. This results in a "invalid_request" after creation.
Is there a way to get the plain password or generate the token a different way?