Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Unnamed Auth routes returns an error when inside a route group #577

Merged
merged 21 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,24 @@ public $filters = [
]
];
```

> **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.

For example, if you configured your routes like so:

```php
$routes->group('accounts', static function($routes) {
service('auth')->routes($routes);
});
```
Then the global `before` filter for `session` should look like so:

```php
public $globals = [
'before' => [
// ...
'session' => ['except' => ['accounts/login*', 'accounts/register', 'accounts/auth/a/*']]
]
]
```
The same should apply for the Rate Limiting.
30 changes: 26 additions & 4 deletions src/Config/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Auth extends BaseConfig

/**
* --------------------------------------------------------------------
* Redirect urLs
* Redirect URLs
* --------------------------------------------------------------------
* The default URL that a user will be redirected to after
* various auth actions. If you need more flexibility you can
Expand Down Expand Up @@ -372,10 +372,32 @@ public function registerRedirect(): string
return $this->getUrl($url);
}

/**
* Accepts a string which can be an absolute URL or
* a named route or just a URI path, and returns the
* full path.
*
* @param string $url an absolute URL or a named route or just URI path
*/
protected function getUrl(string $url): string
sammyskills marked this conversation as resolved.
Show resolved Hide resolved
{
return strpos($url, 'http') === 0
? $url
: rtrim(site_url($url), '/ ');
// To accommodate all url patterns
$final_url = '';

switch (true) {
case strpos($url, 'http://') === 0 || strpos($url, 'https://') === 0: // URL begins with 'http' or 'https'. E.g. http://example.com
$final_url = $url;
break;

case route_to($url) !== false: // URL is a named-route
$final_url = rtrim(url_to($url), '/ ');
break;

default: // URL is a route (URI path)
$final_url = rtrim(site_url($url), '/ ');
break;
}

return $final_url;
}
}
9 changes: 6 additions & 3 deletions src/Config/AuthRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class AuthRoutes extends BaseConfig
'get',
'register',
'RegisterController::registerView',
'register', // Route name
],
[
'post',
Expand All @@ -26,6 +27,7 @@ class AuthRoutes extends BaseConfig
'get',
'login',
'LoginController::loginView',
'login', // Route name
],
[
'post',
Expand Down Expand Up @@ -57,26 +59,27 @@ class AuthRoutes extends BaseConfig
'get',
'logout',
'LoginController::logoutAction',
'logout', // Route name
],
],
'auth-actions' => [
[
'get',
'auth/a/show',
'ActionController::show',
'auth-action-show',
'auth-action-show', // Route name
],
[
'post',
'auth/a/handle',
'ActionController::handle',
'auth-action-handle',
'auth-action-handle', // Route name
],
[
'post',
'auth/a/verify',
'ActionController::verify',
'auth-action-verify',
'auth-action-verify', // Route name
],
],
];
Expand Down