Skip to content

Commit 084bb14

Browse files
authored
Merge pull request #577 from sammyskills/fix/update-named-routes
Fix: Unnamed Auth routes returns an error when inside a route group
2 parents 49887d9 + ed087e3 commit 084bb14

File tree

4 files changed

+64
-17
lines changed

4 files changed

+64
-17
lines changed

docs/customization.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ $routes->get('login', '\App\Controllers\Auth\LoginController::loginView');
2424
$routes->get('register', '\App\Controllers\Auth\RegisterController::registerView');
2525
```
2626

27-
28-
2927
## Custom Redirect URLs
3028

3129
By default, a successful login or register attempt will all redirect to `/`, while a logout action
32-
will redirect to `/login`. You can change the default URLs used within the `Auth` config file:
30+
will redirect to a [named route](https://codeigniter.com/user_guide/incoming/routing.html#using-named-routes "See routing docs") `login` or a *URI path* `/login`. You can change the default URLs used within the `Auth` config file:
3331

3432
```php
3533
public array $redirects = [
@@ -63,13 +61,10 @@ Shield has the following controllers that can be extended to handle
6361
various parts of the authentication process:
6462

6563
- **ActionController** handles the after-login and after-registration actions, like Two Factor Authentication and Email Verification.
66-
6764
- **LoginController** handles the login process.
68-
6965
- **RegisterController** handles the registration process. Overriding this class allows you to customize the User Provider, the User Entity, and the validation rules.
70-
7166
- **MagicLinkController** handles the "lost password" process that allows a user to login with a link sent to their email. This allows you to
72-
override the message that is displayed to a user to describe what is happening, if you'd like to provide more information than simply swapping out the view used.
67+
override the message that is displayed to a user to describe what is happening, if you'd like to provide more information than simply swapping out the view used.
7368

7469
It is not recommended to copy the entire controller into **app/Controllers** and change its namespace. Instead, you should create a new controller that extends
7570
the existing controller and then only override the methods needed. This allows the other methods to stay up to date with any security

docs/install.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,24 @@ public $filters = [
240240
]
241241
];
242242
```
243+
244+
> **Note** If you have grouped or changed the default format of the routes, ensure that your code matches the new format(s) in the `App/Config/Filter.php` file.
245+
246+
For example, if you configured your routes like so:
247+
248+
```php
249+
$routes->group('accounts', static function($routes) {
250+
service('auth')->routes($routes);
251+
});
252+
```
253+
Then the global `before` filter for `session` should look like so:
254+
255+
```php
256+
public $globals = [
257+
'before' => [
258+
// ...
259+
'session' => ['except' => ['accounts/login*', 'accounts/register', 'accounts/auth/a/*']]
260+
]
261+
]
262+
```
263+
The same should apply for the Rate Limiting.

src/Config/Auth.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@ class Auth extends BaseConfig
3939

4040
/**
4141
* --------------------------------------------------------------------
42-
* Redirect urLs
42+
* Redirect URLs
4343
* --------------------------------------------------------------------
44-
* The default URL that a user will be redirected to after
45-
* various auth actions. If you need more flexibility you can
46-
* override the `getUrl()` method to apply any logic you may need.
44+
* The default URL that a user will be redirected to after various auth
45+
* auth actions. This can be either of the following:
46+
*
47+
* 1. An absolute URL. E.g. http://example.com OR https://example.com
48+
* 2. A named route that can be accessed using `route_to()` or `url_to()`
49+
* 3. A URI path within the application. e.g 'admin', 'login', 'expath'
50+
*
51+
* If you need more flexibility you can override the `getUrl()` method
52+
* to apply any logic you may need.
4753
*/
4854
public array $redirects = [
4955
'register' => '/',
@@ -372,10 +378,32 @@ public function registerRedirect(): string
372378
return $this->getUrl($url);
373379
}
374380

381+
/**
382+
* Accepts a string which can be an absolute URL or
383+
* a named route or just a URI path, and returns the
384+
* full path.
385+
*
386+
* @param string $url an absolute URL or a named route or just URI path
387+
*/
375388
protected function getUrl(string $url): string
376389
{
377-
return strpos($url, 'http') === 0
378-
? $url
379-
: rtrim(site_url($url), '/ ');
390+
// To accommodate all url patterns
391+
$final_url = '';
392+
393+
switch (true) {
394+
case strpos($url, 'http://') === 0 || strpos($url, 'https://') === 0: // URL begins with 'http' or 'https'. E.g. http://example.com
395+
$final_url = $url;
396+
break;
397+
398+
case route_to($url) !== false: // URL is a named-route
399+
$final_url = rtrim(url_to($url), '/ ');
400+
break;
401+
402+
default: // URL is a route (URI path)
403+
$final_url = rtrim(site_url($url), '/ ');
404+
break;
405+
}
406+
407+
return $final_url;
380408
}
381409
}

src/Config/AuthRoutes.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class AuthRoutes extends BaseConfig
1414
'get',
1515
'register',
1616
'RegisterController::registerView',
17+
'register', // Route name
1718
],
1819
[
1920
'post',
@@ -26,6 +27,7 @@ class AuthRoutes extends BaseConfig
2627
'get',
2728
'login',
2829
'LoginController::loginView',
30+
'login', // Route name
2931
],
3032
[
3133
'post',
@@ -57,26 +59,27 @@ class AuthRoutes extends BaseConfig
5759
'get',
5860
'logout',
5961
'LoginController::logoutAction',
62+
'logout', // Route name
6063
],
6164
],
6265
'auth-actions' => [
6366
[
6467
'get',
6568
'auth/a/show',
6669
'ActionController::show',
67-
'auth-action-show',
70+
'auth-action-show', // Route name
6871
],
6972
[
7073
'post',
7174
'auth/a/handle',
7275
'ActionController::handle',
73-
'auth-action-handle',
76+
'auth-action-handle', // Route name
7477
],
7578
[
7679
'post',
7780
'auth/a/verify',
7881
'ActionController::verify',
79-
'auth-action-verify',
82+
'auth-action-verify', // Route name
8083
],
8184
],
8285
];

0 commit comments

Comments
 (0)