|
@@ -9,28 +9,28 @@ class CorsControl
|
|
|
{
|
|
{
|
|
|
public function __invoke(ServerRequestInterface $request, callable $next)
|
|
public function __invoke(ServerRequestInterface $request, callable $next)
|
|
|
{
|
|
{
|
|
|
- // Configurações de CORS permissivas
|
|
|
|
|
|
|
+ // Cabeçalhos CORS liberais para todas as origens
|
|
|
$corsHeaders = [
|
|
$corsHeaders = [
|
|
|
- 'Access-Control-Allow-Origin' => '*', // Permite qualquer origem
|
|
|
|
|
- 'Access-Control-Allow-Methods' => '*', // Permite qualquer método
|
|
|
|
|
- 'Access-Control-Allow-Headers' => '*' // Permite qualquer cabeçalho
|
|
|
|
|
|
|
+ 'Access-Control-Allow-Origin' => '*',
|
|
|
|
|
+ 'Access-Control-Allow-Methods' => 'GET, POST',
|
|
|
|
|
+ 'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With, Accept, Origin',
|
|
|
|
|
+ 'Access-Control-Allow-Credentials' => 'true',
|
|
|
|
|
+ 'Access-Control-Max-Age' => '86400', // 24 horas em segundos
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
- // Lida com requisições OPTIONS (preflight)
|
|
|
|
|
|
|
+ // Responde imediatamente a preflight OPTIONS
|
|
|
if ($request->getMethod() === 'OPTIONS') {
|
|
if ($request->getMethod() === 'OPTIONS') {
|
|
|
- return new Response(200, $corsHeaders);
|
|
|
|
|
|
|
+ return new Response(204, $corsHeaders);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // Chama o próximo middleware/controlador
|
|
|
|
|
|
|
+ // Executa o próximo middleware / controlador
|
|
|
$response = $next($request);
|
|
$response = $next($request);
|
|
|
|
|
|
|
|
- // Adiciona cabeçalhos CORS à resposta
|
|
|
|
|
|
|
+ // Injeta os headers CORS na resposta
|
|
|
foreach ($corsHeaders as $header => $value) {
|
|
foreach ($corsHeaders as $header => $value) {
|
|
|
- if ($value) {
|
|
|
|
|
- $response = $response->withHeader($header, $value);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ $response = $response->withHeader($header, $value);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
return $response;
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+}
|