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 $request, AuthenticationException $authException = null): Response
  18. {
  19. // Check if this is an API request (expects JSON response)
  20. if ($request->isXmlHttpRequest() ||
  21. strpos($request->getPathInfo(), '/api/') === 0 ||
  22. $request->headers->get('Accept') === 'application/json' ||
  23. $request->headers->get('Content-Type') === 'application/json') {
  24. return new JsonResponse([
  25. 'success' => false,
  26. 'message' => 'Authentication required',
  27. 'error' => 'UNAUTHORIZED'
  28. ], Response::HTTP_UNAUTHORIZED);
  29. }
  30. // For web requests, redirect to login page (Google OAuth)
  31. $request->getSession()->getFlashBag()->add('note', 'You have to login in order to access this page.');
  32. return new RedirectResponse('/login');
  33. }
  34. }