src/Authentication/AuthenticationSuccessHandler.php line 23

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Authentication;
  4. use Symfony\Component\HttpFoundation\JsonResponse;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
  10. use Symfony\Component\Messenger\MessageBusInterface;
  11. use Symfony\Component\Security\Http\HttpUtils;
  12. use Psr\Log\LoggerInterface;
  13. use App\Message\SyncCustomer;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. final class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler
  16. {
  17.     private $bus;
  18.     public function __construct(HttpUtils $httpUtils, array $options = [], LoggerInterface $logger nullRequestStack $requestMessageBusInterface $busContainerInterface $container)
  19.     {
  20.         $this->httpUtils $httpUtils;
  21.         $this->logger $logger;
  22.         $this->setOptions($options);
  23.         $this->bus $bus;
  24.         $this->container $container;
  25.     }
  26.     public function onAuthenticationSuccess(Request $requestTokenInterface $token): Response
  27.     {
  28.         //On lance la synchronisation du compte client
  29.         // dump($token->getUser()->getCustomer());exit();
  30.         $this->bus->dispatch(new SyncCustomer((int)$token->getUser()->getCustomer()->getId()));
  31.     
  32.         if ($request->isXmlHttpRequest()) {
  33.             return new JsonResponse(['success' => true'username' => $token->getUser()->getUsername()]);
  34.         }
  35.         $cart =  $this->container->get('sylius.context.cart')->getCart();  
  36.         if (null !== $cart->getId()) {
  37.             return  $this->httpUtils->createRedirectResponse($request'sylius_shop_cart_summary');
  38.         }else{
  39.             return  $this->httpUtils->createRedirectResponse($request'sylius_shop_account_dashboard');
  40.         }
  41.        
  42.         return $this->httpUtils->createRedirectResponse($request'sylius_shop_cart_summary');
  43.         
  44.         // $syliusTargetPath = $request->getSession()->get('_security.shop.target_path', null);
  45.         // if (empty($syliusTargetPath) == false) {
  46.         //     return $this->httpUtils->createRedirectResponse($request, $syliusTargetPath);
  47.         // }
  48.         // return parent::onAuthenticationSuccess($request, $token);
  49.     }
  50. }