From 06241a2a47e78d3c2cf5f3d64137331065d330c6 Mon Sep 17 00:00:00 2001 From: PatrickePatate Date: Thu, 6 Mar 2025 15:37:41 +0100 Subject: [PATCH 1/3] redirectLogoutToUrl() --- src/Config/SharpConfigBuilder.php | 8 ++++++++ src/Http/Controllers/Auth/LoginController.php | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/src/Config/SharpConfigBuilder.php b/src/Config/SharpConfigBuilder.php index 13de13589..388cd3f1e 100644 --- a/src/Config/SharpConfigBuilder.php +++ b/src/Config/SharpConfigBuilder.php @@ -58,6 +58,7 @@ class SharpConfigBuilder ], 'auth' => [ 'login_page_url' => null, + 'logout_page_url' => null, 'display_attribute' => 'name', 'login_attribute' => 'email', 'password_attribute' => 'password', @@ -480,6 +481,13 @@ public function redirectLoginToUrl(?string $url): self return $this; } + public function redirectLogoutToUrl(?string $url): self + { + $this->config['auth']['logout_page_url'] = $url; + + return $this; + } + public function loadViteAssets(array|Vite $assets): self { $this->config['assets'][] = $assets instanceof Vite diff --git a/src/Http/Controllers/Auth/LoginController.php b/src/Http/Controllers/Auth/LoginController.php index d36af268e..470ca81f5 100644 --- a/src/Http/Controllers/Auth/LoginController.php +++ b/src/Http/Controllers/Auth/LoginController.php @@ -55,6 +55,10 @@ public function store(LoginRequest $request): RedirectResponse public function destroy(Request $request): RedirectResponse { + if (sharp()->config()->get('auth.logout_page_url')) { + return redirect()->to(sharp()->config()->get('auth.logout_page_url')); + } + Auth::guard(sharp()->config()->get('auth.guard'))->logout(); $request->session()->invalidate(); From 91008464b37547ba54a7a6e05cecc23dfea13959 Mon Sep 17 00:00:00 2001 From: PatrickePatate Date: Thu, 6 Mar 2025 15:41:35 +0100 Subject: [PATCH 2/3] Update docs --- docs/guide/authentication.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/guide/authentication.md b/docs/guide/authentication.md index 564982ad4..958a8e371 100644 --- a/docs/guide/authentication.md +++ b/docs/guide/authentication.md @@ -385,6 +385,7 @@ class SharpServiceProvider extends SharpAppServiceProvider { $config ->redirectLoginToUrl('/my_login') + ->redirectLogoutToUrl('/my_logout') // [...] } } From 797d8fd77b6e4dd3c4f7916fe6fc8ddd68f125d8 Mon Sep 17 00:00:00 2001 From: PatrickePatate Date: Thu, 6 Mar 2025 16:01:56 +0100 Subject: [PATCH 3/3] Improve custom logout url handling --- src/Http/Controllers/Auth/LoginController.php | 4 ++-- tests/Http/Auth/AuthenticationTest.php | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Http/Controllers/Auth/LoginController.php b/src/Http/Controllers/Auth/LoginController.php index 470ca81f5..484f088b6 100644 --- a/src/Http/Controllers/Auth/LoginController.php +++ b/src/Http/Controllers/Auth/LoginController.php @@ -55,8 +55,8 @@ public function store(LoginRequest $request): RedirectResponse public function destroy(Request $request): RedirectResponse { - if (sharp()->config()->get('auth.logout_page_url')) { - return redirect()->to(sharp()->config()->get('auth.logout_page_url')); + if ($logoutPageUrl = sharp()->config()->get('auth.logout_page_url')) { + return redirect()->to($logoutPageUrl); } Auth::guard(sharp()->config()->get('auth.guard'))->logout(); diff --git a/tests/Http/Auth/AuthenticationTest.php b/tests/Http/Auth/AuthenticationTest.php index 0c8667e0d..597f4c796 100644 --- a/tests/Http/Auth/AuthenticationTest.php +++ b/tests/Http/Auth/AuthenticationTest.php @@ -173,3 +173,18 @@ public function setUser(\Illuminate\Contracts\Auth\Authenticatable $user) $this->get('/sharp/s-list/person') ->assertRedirect(route('code16.sharp.login')); }); + +it('allows to use a custom login url', function () { + sharp()->config()->redirectLoginToUrl('/custom-login'); + + $this->get(route('code16.sharp.login')) + ->assertRedirect('/custom-login'); +}); + +it('allows to use a custom logout url', function () { + login(); + sharp()->config()->redirectLogoutToUrl('/custom-logout'); + + $this->post(route('code16.sharp.logout')) + ->assertRedirect('/custom-logout'); +});