diff --git a/src/Filters/AbstractAuthFilter.php b/src/Filters/AbstractAuthFilter.php index 25d6ff416..750f5993a 100644 --- a/src/Filters/AbstractAuthFilter.php +++ b/src/Filters/AbstractAuthFilter.php @@ -30,6 +30,12 @@ public function before(RequestInterface $request, $arguments = null) } if (! auth()->loggedIn()) { + // Set the entrance url to redirect a user after successful login + if (! url_is('login')) { + $session = session(); + $session->setTempdata('beforeLoginUrl', current_url(), 300); + } + return redirect()->route('login'); } diff --git a/tests/Authentication/Filters/GroupFilterTest.php b/tests/Authentication/Filters/GroupFilterTest.php index 2e27e03ae..2690eeaff 100644 --- a/tests/Authentication/Filters/GroupFilterTest.php +++ b/tests/Authentication/Filters/GroupFilterTest.php @@ -31,6 +31,16 @@ public function testFilterNotAuthorized(): void $result->assertSee('Open'); } + public function testFilterNotAuthorizedStoresRedirectToEntranceUrlIntoSession(): void + { + $result = $this->call('get', 'protected-route'); + + $result->assertRedirectTo('/login'); + + $this->assertNotEmpty(session()->getTempdata('beforeLoginUrl')); + $this->assertSame(site_url('protected-route'), session()->getTempdata('beforeLoginUrl')); + } + public function testFilterSuccess(): void { /** @var User $user */ diff --git a/tests/Authentication/Filters/PermissionFilterTest.php b/tests/Authentication/Filters/PermissionFilterTest.php index 77aabdf4c..140bde35b 100644 --- a/tests/Authentication/Filters/PermissionFilterTest.php +++ b/tests/Authentication/Filters/PermissionFilterTest.php @@ -31,6 +31,16 @@ public function testFilterNotAuthorized(): void $result->assertSee('Open'); } + public function testFilterNotAuthorizedStoresRedirectToEntranceUrlIntoSession(): void + { + $result = $this->call('get', 'protected-route'); + + $result->assertRedirectTo('/login'); + + $this->assertNotEmpty(session()->getTempdata('beforeLoginUrl')); + $this->assertSame(site_url('protected-route'), session()->getTempdata('beforeLoginUrl')); + } + public function testFilterSuccess(): void { /** @var User $user */