src/EventListener/AuthenticationSuccessListener.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Device;
  4. use App\Entity\User;
  5. use App\Entity\UserDeviceToken;
  6. use App\Entity\UserProfile;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
  9. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
  10. use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. class AuthenticationSuccessListener
  14. {
  15.     private $em;
  16.     private $jwtManager;
  17.     public function __construct(EntityManagerInterface $emJWTTokenManagerInterface $jwtManager,RequestStack $requestStack)
  18.     {
  19.         $this->em $em;
  20.         $this->jwtManager $jwtManager;
  21.         $this->requestStack $requestStack;
  22.     }
  23.     public function onAuthenticationSuccess(AuthenticationSuccessEvent $event)
  24.     {
  25.         $request $this->requestStack->getCurrentRequest();
  26.         $requestData json_decode($request->getContent(), true);
  27.         $params['system_language'] = $requestData['system_language'] ?? 'uk';
  28.         $params['deviceToken'] = $requestData['device_token'] ?? '';
  29.         $data $event->getData();
  30.         $user $event->getUser();
  31.         if(property_exists($user'isVerified') && !$user->getIsVerified()){
  32.             $event->setData([
  33.                 'status'=>'failed','message'=>'Email not verified'
  34.             ]);
  35.             return true;
  36.         }
  37.         // Fetch user profile data
  38.         $userProfileData $this->em->getRepository(UserProfile::class)->getUserDetailsWithIdOrName(['userName'=> $user?->getUserName()]);
  39.         // Generate a new JWT token
  40.         $user $this->em->getRepository(User::class)->findOneBy(['userName' => $user?->getUserName()]);
  41.         $userId $params['user_id'] = $user->getId();
  42.         $params['status'] = '1';
  43.         
  44.         //Fetch devices
  45.         $deviceCount $this->em->getRepository(UserProfile::class)->getDeviceCount($userId);
  46.         if(!empty($params['deviceToken'])){
  47.             $checkDeviceExist $this->em->getRepository(UserDeviceToken::class)->checkDeviceExist($params);
  48.             if(empty($checkDeviceExist)){
  49.                 $saveUserDevice $this->em->getRepository(UserDeviceToken::class)->saveUserDeviceData($params);
  50.             }
  51.         }
  52.         //update user system language
  53.         if(!empty($params['system_language'])){
  54.             $this->em->getRepository(User::class)->updateUserSystemLanguage($params);
  55.         }
  56.         // Add additional parameters to the response data
  57.         $data = [
  58.             'status' => 'success',
  59.             'message' => 'Login successfully',
  60.             'data' => [
  61.                 'user_id' => $user->getId(),
  62.                 'username' => $user->getUserName(),
  63.                 'email' => $user->getEmail(),
  64.                 'profile_image' => $userProfileData['profileImage'],
  65.                 'contact_number' => $userProfileData['contactNumber'],
  66.                 'first_name' => $userProfileData['firstName'],
  67.                 'last_name' => $userProfileData['lastName'],
  68.                 'deviceCount' => (string)$deviceCount,
  69.             ],
  70.             'token' => $this->jwtManager->create($user)
  71.         ];
  72.         $event->setData($data);
  73.     }
  74.     public function onAuthenticationFailure(AuthenticationFailureEvent $event)
  75.     {
  76.         $data = [
  77.             'status' => 'failure',
  78.             'message' => 'Incorrect credentials'
  79.         ];
  80.         $response = new JsonResponse($data);
  81.         $event->setResponse($response);
  82.     }
  83. }