Skip to content

Commit 0c6f30e

Browse files
authored
Merge pull request #726 from cakephp/3.3-default-login-redirect
Add default URL to AuthenticationComponent::getLoginRedirect()
2 parents 1d82427 + 4619778 commit 0c6f30e

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/Controller/Component/AuthenticationComponent.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,16 @@ public function logout(): ?string
360360
* Leverages the `unauthenticatedRedirect` and `queryParam` options in
361361
* the AuthenticationService.
362362
*
363+
* @param array|string|null $default Default URL to use if no redirect URL is available.
363364
* @return string|null
364365
*/
365-
public function getLoginRedirect(): ?string
366+
public function getLoginRedirect(array|string|null $default = null): ?string
366367
{
367-
$controller = $this->getController();
368+
if (is_array($default)) {
369+
$default = Router::url(['_base' => false] + $default);
370+
}
368371

369-
return $this->getAuthenticationService()->getLoginRedirect($controller->getRequest());
372+
return $this->getAuthenticationService()->getLoginRedirect($this->getController()->getRequest()) ?? $default;
370373
}
371374

372375
/**

tests/TestCase/Controller/Component/AuthenticationComponentTest.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
use Authentication\Test\TestCase\AuthenticationTestCase as TestCase;
2828
use Cake\Controller\ComponentRegistry;
2929
use Cake\Controller\Controller;
30+
use Cake\Core\Configure;
3031
use Cake\Event\Event;
3132
use Cake\Event\EventManager;
3233
use Cake\Http\ServerRequestFactory;
3334
use Cake\ORM\Entity;
35+
use Cake\Routing\Router;
3436
use InvalidArgumentException;
3537
use TestApp\Authentication\InvalidAuthenticationService;
3638
use UnexpectedValueException;
@@ -357,18 +359,33 @@ public function testLogout()
357359
*/
358360
public function testGetLoginRedirect()
359361
{
362+
Configure::write('App.base', '/cakephp');
363+
$url = ['controller' => 'Users', 'action' => 'dashboard'];
364+
Router::createRouteBuilder('/')
365+
->connect('/dashboard', $url);
366+
360367
$this->service->setConfig('queryParam', 'redirect');
361368
$request = $this->request
362369
->withAttribute('identity', $this->identity)
363-
->withAttribute('authentication', $this->service)
364-
->withQueryParams(['redirect' => 'ok/path?value=key']);
370+
->withAttribute('authentication', $this->service);
371+
372+
$controller = new Controller($request);
373+
$registry = new ComponentRegistry($controller);
374+
$component = new AuthenticationComponent($registry);
375+
376+
$result = $component->getLoginRedirect($url);
377+
$this->assertSame('/dashboard', $result);
378+
379+
$request = $request->withQueryParams(['redirect' => 'ok/path?value=key']);
365380

366381
$controller = new Controller($request);
367382
$registry = new ComponentRegistry($controller);
368383
$component = new AuthenticationComponent($registry);
369384

370-
$result = $component->getLoginRedirect();
385+
$result = $component->getLoginRedirect($url);
371386
$this->assertSame('/ok/path?value=key', $result);
387+
388+
Configure::delete('App.base');
372389
}
373390

374391
/**

0 commit comments

Comments
 (0)