From 44291fb2219c6524d67c34c195a9a5c8b4433284 Mon Sep 17 00:00:00 2001 From: RemRem Date: Mon, 13 Nov 2023 17:23:04 +0100 Subject: [PATCH] add OPTIONS request handle --- app/routes.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/routes.php b/app/routes.php index f46dccb..7cfc944 100644 --- a/app/routes.php +++ b/app/routes.php @@ -13,12 +13,24 @@ header("Access-Control-Allow-Credentials: true"); use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\App; +use SLim\Exception\HttpNotFoundException; use gateway\UserGateway; use Config\Token; use Gateway\FileGateway; return function (App $app) { + $app->options('/{routes:.+}', function ($request, $response, $args) { + return $response; + }); + $app->add(function ($request, $handler) { + $response = $handler->handle($request); + return $response + ->withHeader('Access-Control-Allow-Origin', '*') + ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization') + ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS'); + }); + $app->get('/', function (Request $req, Response $res) { $res->getBody()->write('SmartFit-API is working!'); return $res; @@ -227,4 +239,8 @@ return function (App $app) { if($code === -1) return $res->withStatus(500); return $res->withStatus(200); }); + + $app->map(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], '/{routes:.+}', function ($request, $response) { + throw new HttpNotFoundException($request); + }); };