src/Security/DualAuthenticationEntryPoint.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use Symfony\Component\HttpFoundation\JsonResponse;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  8. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  9. use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
  10. class DualAuthenticationEntryPoint implements AuthenticationEntryPointInterface
  11. {
  12.     private $urlGenerator;
  13.     public function __construct(UrlGeneratorInterface $urlGenerator)
  14.     {
  15.         $this->urlGenerator $urlGenerator;
  16.     }
  17.     public function start(Request $requestAuthenticationException $authException null): Response
  18.     {
  19.         // Check if this is an API request (expects JSON response)
  20.         if ($request->isXmlHttpRequest() || 
  21.             strpos($request->getPathInfo(), '/api/') === ||
  22.             $request->headers->get('Accept') === 'application/json' ||
  23.             $request->headers->get('Content-Type') === 'application/json') {
  24.             
  25.             return new JsonResponse([
  26.                 'success' => false,
  27.                 'message' => 'Authentication required',
  28.                 'error' => 'UNAUTHORIZED'
  29.             ], Response::HTTP_UNAUTHORIZED);
  30.         }
  31.         // For web requests, redirect to login page (Google OAuth)
  32.         $request->getSession()->getFlashBag()->add('note''You have to login in order to access this page.');
  33.         return new RedirectResponse('/login');
  34.     }
  35. }