src/Controller/Gate2Controller.php line 16

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Cookie;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. class Gate2Controller extends AbstractController
  11. {
  12.     #[Route('/gate2'name'app_gate2'methods: ['GET'])]
  13.     public function index(Request $request): Response
  14.     {
  15.         $firstIdCookie $request->cookies->get('firstid');
  16.         return $this->render('gate/gate2.html.twig', [
  17.             'hasFirstId' => !empty($firstIdCookie),
  18.             'site_name' => 'First ID Demo'
  19.         ]);
  20.     }
  21.     #[Route('/gate2/redirect'name'app_gate2_redirect'methods: ['GET'])]
  22.     public function initiateFirstIdRedirect(Request $request): RedirectResponse
  23.     {
  24.         $host $request->getSchemeAndHttpHost();
  25.         $redirectHost urlencode($host '/first-id');
  26.         $redirectUri urlencode($host '/gate2');
  27.         return new RedirectResponse(
  28.             "https://gate.preprod.first-id.fr?redirectHost={$redirectHost}&redirectUri={$redirectUri}",
  29.             Response::HTTP_FOUND
  30.         );
  31.     }
  32.     #[Route('/first-id'name'app_first_id'methods: ['GET'])]
  33.     public function handleFirstId(Request $request): RedirectResponse
  34.     {
  35.         if ($request->query->has('firstId')) {
  36.             $firstId = (string) $request->query->get('firstId');
  37.             $target $request->query->get('redirectUri');
  38.             $targetUrl is_string($target) && $target !== ''
  39.                 urldecode($target)
  40.                 : $this->generateUrl('app_gate2');
  41.             $response $this->redirect($targetUrl);
  42.             $response->headers->setCookie(
  43.                 new Cookie(
  44.                     'firstid',
  45.                     $firstId,
  46.                     time() + (60 60 24 180),
  47.                     '/',
  48.                     null,
  49.                     true,
  50.                     false,
  51.                     false,
  52.                     Cookie::SAMESITE_NONE
  53.                 )
  54.             );
  55.             return $response;
  56.         }
  57.         return $this->redirectToRoute('app_gate2');
  58.     }
  59. }