From f35602c7b2309032bec2904603af53043b8ec8fd Mon Sep 17 00:00:00 2001 From: Tofandel Date: Mon, 1 Apr 2024 20:20:15 +0200 Subject: [PATCH] Default login redirect to admin url --- src/Facades/RouteHelpers.php | 16 +++++++++++ src/Helpers/RouteHelpers.php | 12 ++++++++ .../Controllers/Admin/LoginController.php | 3 +- .../Admin/ResetPasswordController.php | 3 +- .../Middleware/RedirectIfAuthenticated.php | 3 +- tests/unit/Helpers/RouteHelpersTest.php | 28 +++++++++++++++++++ 6 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 src/Facades/RouteHelpers.php create mode 100644 src/Helpers/RouteHelpers.php create mode 100644 tests/unit/Helpers/RouteHelpersTest.php diff --git a/src/Facades/RouteHelpers.php b/src/Facades/RouteHelpers.php new file mode 100644 index 000000000..3f1e6edf4 --- /dev/null +++ b/src/Facades/RouteHelpers.php @@ -0,0 +1,16 @@ +config = $config; $this->middleware('twill_guest', ['except' => 'logout']); - $this->redirectTo = $config->get('twill.auth_login_redirect_path', '/'); + $this->redirectTo = RouteHelpers::getAuthRedirectPath(); } /** diff --git a/src/Http/Controllers/Admin/ResetPasswordController.php b/src/Http/Controllers/Admin/ResetPasswordController.php index b589dde0c..582b1d364 100644 --- a/src/Http/Controllers/Admin/ResetPasswordController.php +++ b/src/Http/Controllers/Admin/ResetPasswordController.php @@ -2,6 +2,7 @@ namespace A17\Twill\Http\Controllers\Admin; +use A17\Twill\Facades\RouteHelpers; use Illuminate\Config\Repository as Config; use Illuminate\Foundation\Auth\ResetsPasswords; use Illuminate\Http\Request; @@ -59,7 +60,7 @@ public function __construct(Config $config, Redirector $redirector, ViewFactory $this->viewFactory = $viewFactory; $this->config = $config; - $this->redirectTo = $this->config->get('twill.auth_login_redirect_path', '/'); + $this->redirectTo = RouteHelpers::getAuthRedirectPath(); $this->middleware('twill_guest'); } diff --git a/src/Http/Middleware/RedirectIfAuthenticated.php b/src/Http/Middleware/RedirectIfAuthenticated.php index 4091fb13f..21e51e8af 100644 --- a/src/Http/Middleware/RedirectIfAuthenticated.php +++ b/src/Http/Middleware/RedirectIfAuthenticated.php @@ -2,6 +2,7 @@ namespace A17\Twill\Http\Middleware; +use A17\Twill\Facades\RouteHelpers; use Closure; use Illuminate\Config\Repository as Config; use Illuminate\Contracts\Auth\Factory as AuthFactory; @@ -40,7 +41,7 @@ public function __construct(AuthFactory $authFactory, Redirector $redirector, Co public function handle($request, Closure $next, $guard = 'twill_users') { if ($this->authFactory->guard($guard)->check()) { - return $this->redirector->to($this->config->get('twill.auth_login_redirect_path', '/')); + return $this->redirector->to(RouteHelpers::getAuthRedirectPath()); } return $next($request); diff --git a/tests/unit/Helpers/RouteHelpersTest.php b/tests/unit/Helpers/RouteHelpersTest.php new file mode 100644 index 000000000..a386f77fd --- /dev/null +++ b/tests/unit/Helpers/RouteHelpersTest.php @@ -0,0 +1,28 @@ + null, 'twill.admin_app_url' => null, 'twill.admin_app_path' => null]); + + $this->assertEquals('/admin', RouteHelpers::getAuthRedirectPath()); + + config(['twill.admin_app_path' => 'twill']); + + $this->assertEquals('/twill', RouteHelpers::getAuthRedirectPath()); + + config(['twill.admin_app_url' => 'https://admin.example.com/']); + + $this->assertEquals('https://admin.example.com/twill', RouteHelpers::getAuthRedirectPath()); + + config(['twill.auth_login_redirect_path' => '/redirect']); + + $this->assertEquals('/redirect', RouteHelpers::getAuthRedirectPath()); + } +}