Skip to content

Commit 5201fe0

Browse files
committed
feat: possibility to use only in routes configuration
1 parent ee028df commit 5201fe0

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

src/Auth.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use CodeIgniter\Shield\Config\Auth as AuthConfig;
2121
use CodeIgniter\Shield\Entities\User;
2222
use CodeIgniter\Shield\Models\UserModel;
23+
use InvalidArgumentException;
2324

2425
/**
2526
* Facade for Authentication
@@ -134,13 +135,27 @@ public function authenticate(array $credentials): Result
134135
*/
135136
public function routes(RouteCollection &$routes, array $config = []): void
136137
{
138+
if (isset($config['only'], $config['except'])) {
139+
throw new InvalidArgumentException(
140+
'The "only" and "except" options cannot be used at the same time.',
141+
);
142+
}
143+
137144
$authRoutes = config('AuthRoutes')->routes;
138145

139146
$namespace = $config['namespace'] ?? 'CodeIgniter\Shield\Controllers';
140147

141148
$routes->group('/', ['namespace' => $namespace], static function (RouteCollection $routes) use ($authRoutes, $config): void {
142149
foreach ($authRoutes as $name => $row) {
143-
if (! isset($config['except']) || ! in_array($name, $config['except'], true)) {
150+
$shouldInclude = true;
151+
152+
if (isset($config['only'])) {
153+
$shouldInclude = in_array($name, $config['only'], true);
154+
} elseif (isset($config['except'])) {
155+
$shouldInclude = ! in_array($name, $config['except'], true);
156+
}
157+
158+
if ($shouldInclude) {
144159
foreach ($row as $params) {
145160
$options = isset($params[3])
146161
? ['as' => $params[3]]

0 commit comments

Comments
 (0)