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') // [...] } } 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..484f088b6 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 ($logoutPageUrl = sharp()->config()->get('auth.logout_page_url')) { + return redirect()->to($logoutPageUrl); + } + Auth::guard(sharp()->config()->get('auth.guard'))->logout(); $request->session()->invalidate(); 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'); +});