Skip to content

Commit ab728cd

Browse files
committed
Set the redirect query param only for GET requests.
Currently if the session expires for a logged in user and they click on a link for "Delete" action, after login they get redirected to the delete action with a GET request, resulting in an exception since that action is only accessible with POST/DELETE request.
1 parent 93ef8a1 commit ab728cd

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/AuthenticationService.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,14 +383,20 @@ public function buildIdentity(ArrayAccess|array $identityData): IdentityInterfac
383383
*/
384384
public function getUnauthenticatedRedirectUrl(ServerRequestInterface $request): ?string
385385
{
386-
$param = $this->getConfig('queryParam');
387386
$target = $this->getConfig('unauthenticatedRedirect');
388387
if ($target === null) {
389388
return null;
390389
}
390+
391391
if (is_array($target) && class_exists(Router::class)) {
392392
$target = Router::url($target);
393393
}
394+
395+
if ($request->getMethod() !== 'GET') {
396+
return $target;
397+
}
398+
399+
$param = $this->getConfig('queryParam');
394400
if ($param === null) {
395401
return $target;
396402
}

tests/TestCase/AuthenticationServiceTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,22 @@ public function testGetUnauthenticatedRedirectUrl()
847847
);
848848
}
849849

850+
public function testGetUnauthenticatedRedirectUrlForPost()
851+
{
852+
$service = new AuthenticationService();
853+
$service->setConfig('unauthenticatedRedirect', '/users/login');
854+
$service->setConfig('queryParam', 'redirect');
855+
856+
$request = ServerRequestFactory::fromGlobals(
857+
['REQUEST_URI' => '/secrets', 'REQUEST_METHOD' => 'POST'],
858+
);
859+
$this->assertSame(
860+
'/users/login',
861+
$service->getUnauthenticatedRedirectUrl($request),
862+
'Redirect query param should be only set for GET requests',
863+
);
864+
}
865+
850866
public function testGetUnauthenticatedRedirectUrlAsArray()
851867
{
852868
Router::fullBaseUrl('http://localhost');

0 commit comments

Comments
 (0)