From d2c20b942356afb6bf0846acf00edf422ec6c9dd Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 29 Jul 2025 14:38:19 +0200 Subject: [PATCH] access token on log in --- src/EventSubscriber/LoginSubscriber.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/EventSubscriber/LoginSubscriber.php b/src/EventSubscriber/LoginSubscriber.php index 66ab103..5438de6 100644 --- a/src/EventSubscriber/LoginSubscriber.php +++ b/src/EventSubscriber/LoginSubscriber.php @@ -4,6 +4,8 @@ namespace App\EventSubscriber; use App\Entity\User; use Doctrine\ORM\EntityManagerInterface; +use League\Bundle\OAuth2ServerBundle\Model\AccessToken; +use League\Bundle\OAuth2ServerBundle\Model\Client; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Security\Http\Event\LoginSuccessEvent; @@ -30,8 +32,20 @@ class LoginSubscriber implements EventSubscriberInterface if($user) { $user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $user->getUserIdentifier()]); $user->setLastConnection(new \DateTime('now', new \DateTimeZone('Europe/Paris'))); - $this->entityManager->persist($user); - $this->entityManager->flush(); + + $easySolution = $this->entityManager->getRepository(Client::class)->findOneBy(['name' => 'EasySolution']); + if($easySolution) { + $accessToken = new AccessToken( + identifier: bin2hex(random_bytes(40)), // Generate unique identifier + expiry: new \DateTimeImmutable('+1 hour', new \DateTimeZone('Europe/Paris')), + client: $easySolution, + userIdentifier: $user->getUserIdentifier(), + scopes: ['email profile openid apps:easySolutions'] // Empty array if no specific scopes needed + ); + $this->entityManager->persist($user); + $this->entityManager->persist($accessToken); + $this->entityManager->flush(); + } } } }