From 5201fe0d3b607af5689711aaed13df817970c270 Mon Sep 17 00:00:00 2001 From: Dimitri Sitchet Tomkeu Date: Mon, 20 Apr 2026 19:55:44 +0100 Subject: [PATCH 1/3] feat: possibility to use `only` in routes configuration --- src/Auth.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Auth.php b/src/Auth.php index fedee8a97..e47c04492 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -20,6 +20,7 @@ use CodeIgniter\Shield\Config\Auth as AuthConfig; use CodeIgniter\Shield\Entities\User; use CodeIgniter\Shield\Models\UserModel; +use InvalidArgumentException; /** * Facade for Authentication @@ -134,13 +135,27 @@ public function authenticate(array $credentials): Result */ public function routes(RouteCollection &$routes, array $config = []): void { + if (isset($config['only'], $config['except'])) { + throw new InvalidArgumentException( + 'The "only" and "except" options cannot be used at the same time.', + ); + } + $authRoutes = config('AuthRoutes')->routes; $namespace = $config['namespace'] ?? 'CodeIgniter\Shield\Controllers'; $routes->group('/', ['namespace' => $namespace], static function (RouteCollection $routes) use ($authRoutes, $config): void { foreach ($authRoutes as $name => $row) { - if (! isset($config['except']) || ! in_array($name, $config['except'], true)) { + $shouldInclude = true; + + if (isset($config['only'])) { + $shouldInclude = in_array($name, $config['only'], true); + } elseif (isset($config['except'])) { + $shouldInclude = ! in_array($name, $config['except'], true); + } + + if ($shouldInclude) { foreach ($row as $params) { $options = isset($params[3]) ? ['as' => $params[3]] From b79322544c696b1cfea0d8960b2fd63fb69de88f Mon Sep 17 00:00:00 2001 From: Dimitri Sitchet Tomkeu Date: Mon, 20 Apr 2026 19:56:46 +0100 Subject: [PATCH 2/3] test: tests for `only` options in routes configuartion --- tests/Unit/AuthRoutesTest.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/Unit/AuthRoutesTest.php b/tests/Unit/AuthRoutesTest.php index d4e568121..191331a7b 100644 --- a/tests/Unit/AuthRoutesTest.php +++ b/tests/Unit/AuthRoutesTest.php @@ -16,6 +16,7 @@ use CodeIgniter\CodeIgniter; use CodeIgniter\Router\RouteCollection; use CodeIgniter\Shield\Auth; +use InvalidArgumentException; use Tests\Support\TestCase; /** @@ -65,6 +66,36 @@ public function testRoutesExcept(): void $this->assertArrayHasKey('auth/a/show', $routes); } + public function testRoutesOnly(): void + { + $collection = single_service('routes'); + $auth = service('auth'); + + $auth->routes($collection, ['only' => ['login']]); + + if (version_compare(CodeIgniter::CI_VERSION, '4.5') >= 0) { + $routes = $collection->getRoutes('GET'); + } else { + $routes = $collection->getRoutes('get'); + } + + $this->assertArrayHasKey('login', $routes); + $this->assertArrayNotHasKey('register', $routes); + $this->assertArrayNotHasKey('login/magic-link', $routes); + $this->assertArrayNotHasKey('logout', $routes); + $this->assertArrayNotHasKey('auth/a/show', $routes); + } + + public function testRoutesUseOnlyAndExceptOptionsSimultaneous(): void + { + $this->expectException(InvalidArgumentException::class); + + $collection = single_service('routes'); + $auth = service('auth'); + + $auth->routes($collection, ['only' => ['login'], 'except' => ['register']]); + } + public function testRoutesCustomNamespace(): void { $collection = single_service('routes'); From 0cc5dacc33cd384cceb689bd3a35f2756db95f17 Mon Sep 17 00:00:00 2001 From: Dimitri Sitchet Tomkeu Date: Mon, 20 Apr 2026 19:58:03 +0100 Subject: [PATCH 3/3] docs: add docs for `only` options in routes configuartion --- docs/customization/route_config.md | 8 ++++++++ docs/quick_start_guide/using_session_auth.md | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/docs/customization/route_config.md b/docs/customization/route_config.md index 7b06c7d70..f0b05f619 100644 --- a/docs/customization/route_config.md +++ b/docs/customization/route_config.md @@ -17,6 +17,14 @@ $routes->get('login', '\App\Controllers\Auth\LoginController::loginView'); $routes->get('register', '\App\Controllers\Auth\RegisterController::registerView'); ``` +If you only need a specific route (or a small set of routes), you can use the `only` option instead + +```php +service('auth')->routes($routes, ['only' => ['login']]); +``` + +In this case, you must manage all other routes manually. + After customization, check your routes with the [spark routes](https://codeigniter.com/user_guide/incoming/routing.html#spark-routes) command. ## Change Namespace diff --git a/docs/quick_start_guide/using_session_auth.md b/docs/quick_start_guide/using_session_auth.md index 4613fabac..8b72d56f7 100644 --- a/docs/quick_start_guide/using_session_auth.md +++ b/docs/quick_start_guide/using_session_auth.md @@ -89,6 +89,14 @@ $routes->get('login', '\App\Controllers\Auth\LoginController::loginView'); $routes->get('register', '\App\Controllers\Auth\RegisterController::registerView'); ``` +If you only need a specific route (or a small set of routes), you can use the `only` option instead + +```php +service('auth')->routes($routes, ['only' => ['login']]); +``` + +In this case, you must manage all other routes manually. + Check your routes with the [spark routes](https://codeigniter.com/user_guide/incoming/routing.html#spark-routes) command.