Skip to content

allow unauthenticatedRedirect as array cake style. #714

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

Merged
merged 2 commits into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions docs/en/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ define the ``AuthenticationService`` it wants to use. Add the following method t

// Define where users should be redirected to when they are not authenticated
$service->setConfig([
'unauthenticatedRedirect' => Router::url([
'unauthenticatedRedirect' => [
'prefix' => false,
'plugin' => null,
'plugin' => false,
'controller' => 'Users',
'action' => 'login',
]),
],
'queryParam' => 'redirect',
]);

Expand Down
4 changes: 4 additions & 0 deletions src/AuthenticationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Authentication\Identifier\IdentifierCollection;
use Authentication\Identifier\IdentifierInterface;
use Cake\Core\InstanceConfigTrait;
use Cake\Routing\Router;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand Down Expand Up @@ -372,6 +373,9 @@ public function getUnauthenticatedRedirectUrl(ServerRequestInterface $request):
if ($target === null) {
return null;
}
if (is_array($target) && class_exists(Router::class)) {
$target = Router::url($target);
}
if ($param === null) {
return $target;
}
Expand Down
25 changes: 25 additions & 0 deletions tests/TestCase/AuthenticationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Cake\Http\ServerRequest;
use Cake\Http\ServerRequestFactory;
use Cake\I18n\DateTime;
use Cake\Routing\Router;
use InvalidArgumentException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -818,6 +819,30 @@ public function testGetUnauthenticatedRedirectUrl()
);
}

public function testGetUnauthenticatedRedirectUrlAsArray()
{
Router::fullBaseUrl('http://localhost');

$builder = Router::createRouteBuilder('/');
$builder->connect(
'/login',
['controller' => 'Users', 'action' => 'login'],
['_name' => 'login'],
);

$service = new AuthenticationService();
$request = ServerRequestFactory::fromGlobals(
['REQUEST_URI' => '/secrets'],
);
$service->setConfig('unauthenticatedRedirect', [
'prefix' => false,
'plugin' => false,
'controller' => 'Users',
'action' => 'login',
]);
$this->assertSame('/login', $service->getUnauthenticatedRedirectUrl($request));
}

public function testGetUnauthenticatedRedirectUrlWithBasePath()
{
$request = ServerRequestFactory::fromGlobals(
Expand Down