From e922392cb1c66bbd6264420ee062013045cd63b5 Mon Sep 17 00:00:00 2001 From: irbidnet internal lab Date: Sun, 20 Aug 2023 21:35:06 +0300 Subject: [PATCH] feat: redirect after login to entrance url --- src/Config/Auth.php | 3 ++- src/Filters/SessionAuth.php | 5 +++++ tests/Authentication/Filters/SessionFilterTest.php | 8 ++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) 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..31b2b007c 100644 --- a/tests/Authentication/Filters/SessionFilterTest.php +++ b/tests/Authentication/Filters/SessionFilterTest.php @@ -83,4 +83,12 @@ public function testBlocksInactiveUsers(): void setting('Auth.actions', ['register' => null]); } + + public function testStoreRedirectsToEntraceUrlIntoSession(): void + { + $result = $this->call('get', 'protected-route'); + $result->assertRedirectTo('/login'); + $this->assertNotEmpty($_SESSION['beforeLoginUrl']); + $this->assertSame(site_url('protected-route'), $_SESSION['beforeLoginUrl']); + } }