Skip to content
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
8 changes: 7 additions & 1 deletion src/AuthenticationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,20 @@ public function buildIdentity(ArrayAccess|array $identityData): IdentityInterfac
*/
public function getUnauthenticatedRedirectUrl(ServerRequestInterface $request): ?string
{
$param = $this->getConfig('queryParam');
$target = $this->getConfig('unauthenticatedRedirect');
if ($target === null) {
return null;
}

if (is_array($target) && class_exists(Router::class)) {
$target = Router::url($target);
}

if ($request->getMethod() !== 'GET') {
return $target;
}

$param = $this->getConfig('queryParam');
if ($param === null) {
return $target;
}
Expand Down
16 changes: 16 additions & 0 deletions tests/TestCase/AuthenticationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,22 @@ public function testGetUnauthenticatedRedirectUrl()
);
}

public function testGetUnauthenticatedRedirectUrlForPost()
{
$service = new AuthenticationService();
$service->setConfig('unauthenticatedRedirect', '/users/login');
$service->setConfig('queryParam', 'redirect');

$request = ServerRequestFactory::fromGlobals(
['REQUEST_URI' => '/secrets', 'REQUEST_METHOD' => 'POST'],
);
$this->assertSame(
'/users/login',
$service->getUnauthenticatedRedirectUrl($request),
'Redirect query param should be only set for GET requests',
);
}

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