54 lines
1.4 KiB
Markdown
54 lines
1.4 KiB
Markdown
|
|
https://codeigniter.com/user_guide/libraries/cors.html
|
||
|
|
|
||
|
|
app/Config/Filters.php
|
||
|
|
```php
|
||
|
|
public array $filters = [
|
||
|
|
// ...
|
||
|
|
'cors' => [
|
||
|
|
'before' => ['api/*'],
|
||
|
|
'after' => ['api/*'],
|
||
|
|
],
|
||
|
|
];
|
||
|
|
|
||
|
|
```
|
||
|
|
|
||
|
|
app/Config/Routes.php
|
||
|
|
```php
|
||
|
|
// API routes (put below service('auth'))
|
||
|
|
|
||
|
|
// For Cors preflight check
|
||
|
|
$routes->group('', ['filter' => 'cors'], static function (RouteCollection $routes) {
|
||
|
|
$routes->options('api/(:any)', static function () {});
|
||
|
|
});
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
$routes->group("api", ["filter" => "cors", "namespace" => "App\Controllers\Api"], function($routes) {
|
||
|
|
$routes->post("register", "AuthController::register");
|
||
|
|
$routes->post("login", "AuthController::login", ["filter"=>"cors"]);
|
||
|
|
$routes->get("profile", "AuthController::profile");
|
||
|
|
$routes->get("logout", "AuthController::logout", ["filter"=>"apiauth"]);
|
||
|
|
|
||
|
|
$routes->get("invalid-access", "AuthController::accessDenied");
|
||
|
|
|
||
|
|
$routes->get("test2" , "AuthController::test2");
|
||
|
|
|
||
|
|
# WORKS
|
||
|
|
$routes->get("test" , "TestController::test");
|
||
|
|
//$routes->post("add-project", "ProjectController::addProject");
|
||
|
|
//$routes->get("list-projects", "ProjectController::listProjects");
|
||
|
|
//$routes->delete("delete-project/(:num)", "ProjectController::deleteProject/$1");
|
||
|
|
});
|
||
|
|
```
|
||
|
|
|
||
|
|
app/Config/Cors.php
|
||
|
|
```php
|
||
|
|
|
||
|
|
'allowedOrigins' => ['*'],
|
||
|
|
'allowedOriginsPatterns' => ['http://.*','https://.*'],
|
||
|
|
'allowedMethods' => ['GET', 'POST', 'PUT', 'DELETE'],
|
||
|
|
'allowedHeaders' => ['*'],
|
||
|
|
'maxAge' => 7200,
|
||
|
|
|
||
|
|
```
|