<?php
namespace App\Security;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
class DualAuthenticationEntryPoint implements AuthenticationEntryPointInterface
{
private $urlGenerator;
public function __construct(UrlGeneratorInterface $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}
public function start(Request $request, AuthenticationException $authException = null): Response
{
// Check if this is an API request (expects JSON response)
if ($request->isXmlHttpRequest() ||
strpos($request->getPathInfo(), '/api/') === 0 ||
$request->headers->get('Accept') === 'application/json' ||
$request->headers->get('Content-Type') === 'application/json') {
return new JsonResponse([
'success' => false,
'message' => 'Authentication required',
'error' => 'UNAUTHORIZED'
], Response::HTTP_UNAUTHORIZED);
}
// For web requests, redirect to login page (Google OAuth)
$request->getSession()->getFlashBag()->add('note', 'You have to login in order to access this page.');
return new RedirectResponse('/login');
}
}