src/Controller/IndexController.php line 69

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Gesdinet\JWTRefreshTokenBundle\Entity\RefreshToken;
  4. use Hslavich\OneloginSamlBundle\Security\Authentication\Token\SamlToken;
  5. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
  6. use Lexik\Bundle\JWTAuthenticationBundle\Events;
  7. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Exception\BadRequestException;
  13. use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
  14. class IndexController extends AbstractController
  15. {
  16.     /**
  17.      * @var EventDispatcherInterface
  18.      */
  19.     private $dispatcher;
  20.     /**
  21.      * IndexController constructor.
  22.      * @param EventDispatcherInterface $dispatcher
  23.      */
  24.     public function __construct(EventDispatcherInterface $dispatcher)
  25.     {
  26.         $this->dispatcher $dispatcher;
  27.     }
  28.     /**
  29.      * @Route("/check-auth", name="check_auth")
  30.      * @param JWTTokenManagerInterface $JWTManager
  31.      * @return JsonResponse
  32.      */
  33.     public function checkAuth(JWTTokenManagerInterface $JWTManager): JsonResponse
  34.     {
  35.         // get logged in user from token storage
  36.         $tokenStorage $this->get('security.token_storage')->getToken();
  37.         if(!$tokenStorage instanceof SamlToken) return new JsonResponse(['status' => false]);
  38.         $user $tokenStorage->getUser();
  39.         if ($user) {
  40.             $jwt $JWTManager->create($user);
  41.             $oldRefreshToken $this->getDoctrine()->getRepository(RefreshToken::class)->findOneBy(['username' => $user->getUsername()]);
  42.             if ( $oldRefreshToken instanceof RefreshToken) {
  43.                 $payload = [
  44.                     'token' => $jwt,
  45.                     'refresh_token' => $oldRefreshToken->getRefreshToken()
  46.                 ];
  47.             } else {
  48.                 $response = new JsonResponse();
  49.                 $event    = new AuthenticationSuccessEvent(['token' => $jwt], $user$response);
  50.                 $this->dispatcher->dispatch($eventEvents::AUTHENTICATION_SUCCESS);
  51.                 $payload $event->getData();
  52.             }
  53.             return new JsonResponse($payload);
  54.         }
  55.         return new JsonResponse(['status' => false]);
  56.     }
  57.     /**
  58.      * @Route("/{reactRouting}", name="home", defaults={"reactRouting": null}, methods={"GET"})
  59.      * @Route("/{path}", name="all_router", requirements={"path" = ".+"}, priority=-1, methods={"GET"})
  60.      */
  61.     public function index()
  62.     {
  63.         return $this->render('index/index.html.twig', []);
  64.     }
  65.     /**
  66.      * @Route("/admin", name="app_dashboard")
  67.      */
  68.     public function admin(): Response
  69.     {
  70.         return $this->render('index/dashboard.html.twig', []);
  71.     }
  72. }