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
9 changes: 6 additions & 3 deletions src/Controller/Component/AuthenticationComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,16 @@ public function logout(): ?string
* Leverages the `unauthenticatedRedirect` and `queryParam` options in
* the AuthenticationService.
*
* @param array|string|null $default Default URL to use if no redirect URL is available.
* @return string|null
*/
public function getLoginRedirect(): ?string
public function getLoginRedirect(array|string|null $default = null): ?string
{
$controller = $this->getController();
if (is_array($default)) {
$default = Router::url(['_base' => false] + $default);
}

return $this->getAuthenticationService()->getLoginRedirect($controller->getRequest());
return $this->getAuthenticationService()->getLoginRedirect($this->getController()->getRequest()) ?? $default;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
use Authentication\Test\TestCase\AuthenticationTestCase as TestCase;
use Cake\Controller\ComponentRegistry;
use Cake\Controller\Controller;
use Cake\Core\Configure;
use Cake\Event\Event;
use Cake\Event\EventManager;
use Cake\Http\ServerRequestFactory;
use Cake\ORM\Entity;
use Cake\Routing\Router;
use InvalidArgumentException;
use TestApp\Authentication\InvalidAuthenticationService;
use UnexpectedValueException;
Expand Down Expand Up @@ -357,18 +359,33 @@ public function testLogout()
*/
public function testGetLoginRedirect()
{
Configure::write('App.base', '/cakephp');
$url = ['controller' => 'Users', 'action' => 'dashboard'];
Router::createRouteBuilder('/')
->connect('/dashboard', $url);

$this->service->setConfig('queryParam', 'redirect');
$request = $this->request
->withAttribute('identity', $this->identity)
->withAttribute('authentication', $this->service)
->withQueryParams(['redirect' => 'ok/path?value=key']);
->withAttribute('authentication', $this->service);

$controller = new Controller($request);
$registry = new ComponentRegistry($controller);
$component = new AuthenticationComponent($registry);

$result = $component->getLoginRedirect($url);
$this->assertSame('/dashboard', $result);

$request = $request->withQueryParams(['redirect' => 'ok/path?value=key']);

$controller = new Controller($request);
$registry = new ComponentRegistry($controller);
$component = new AuthenticationComponent($registry);

$result = $component->getLoginRedirect();
$result = $component->getLoginRedirect($url);
$this->assertSame('/ok/path?value=key', $result);

Configure::delete('App.base');
}

/**
Expand Down