diff --git a/src/Config/Auth.php b/src/Config/Auth.php index 1c7d57902..792a8ba83 100644 --- a/src/Config/Auth.php +++ b/src/Config/Auth.php @@ -409,7 +409,8 @@ class Auth extends BaseConfig */ public function loginRedirect(): string { - $url = setting('Auth.redirects')['login']; + $session = session(); + $url = $session->getTempdata('beforeLoginUrl') ?? setting('Auth.redirects')['login']; return $this->getUrl($url); } diff --git a/src/Filters/SessionAuth.php b/src/Filters/SessionAuth.php index d95f89c07..6a8a0d8fe 100644 --- a/src/Filters/SessionAuth.php +++ b/src/Filters/SessionAuth.php @@ -75,6 +75,11 @@ public function before(RequestInterface $request, $arguments = null) ->with('error', $authenticator->getPendingMessage()); } + if (! url_is('login')) { + $session = session(); + $session->setTempdata('beforeLoginUrl', current_url(), 300); + } + return redirect()->route('login'); } diff --git a/tests/Authentication/Filters/SessionFilterTest.php b/tests/Authentication/Filters/SessionFilterTest.php index 759a88535..1b379ccc3 100644 --- a/tests/Authentication/Filters/SessionFilterTest.php +++ b/tests/Authentication/Filters/SessionFilterTest.php @@ -83,4 +83,15 @@ public function testBlocksInactiveUsers(): void setting('Auth.actions', ['register' => null]); } + + public function testStoreRedirectsToEntraceUrlIntoSession(): void + { + $result = $this->call('get', 'protected-route'); + + $result->assertRedirectTo('/login'); + + $session = session(); + $this->assertNotEmpty($session->get('beforeLoginUrl')); + $this->assertSame(site_url('protected-route'), $session->get('beforeLoginUrl')); + } }