Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default login redirect to admin url #2569

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/twill.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
|--------------------------------------------------------------------------
|
*/
'auth_login_redirect_path' => '/',
'auth_login_redirect_path' => null,

'templates_on_frontend_domain' => false,

Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/Admin/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace A17\Twill\Http\Controllers\Admin;

use A17\Twill\Facades\TwillRoutes;
use A17\Twill\Http\Requests\Admin\OauthRequest;
use A17\Twill\Models\User;
use A17\Twill\Repositories\UserRepository;
use Carbon\Carbon;
use Illuminate\Auth\AuthManager;
Expand Down Expand Up @@ -81,7 +81,7 @@ public function __construct(
$this->config = $config;

$this->middleware('twill_guest', ['except' => 'logout']);
$this->redirectTo = $config->get('twill.auth_login_redirect_path', '/');
$this->redirectTo = TwillRoutes::getAuthRedirectPath();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Http/Controllers/Admin/ResetPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace A17\Twill\Http\Controllers\Admin;

use A17\Twill\Facades\TwillRoutes;
use Illuminate\Config\Repository as Config;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -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 = TwillRoutes::getAuthRedirectPath();
$this->middleware('twill_guest');
}

Expand Down
3 changes: 2 additions & 1 deletion src/Http/Middleware/RedirectIfAuthenticated.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace A17\Twill\Http\Middleware;

use A17\Twill\Facades\TwillRoutes;
use Closure;
use Illuminate\Config\Repository as Config;
use Illuminate\Contracts\Auth\Factory as AuthFactory;
Expand Down Expand Up @@ -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(TwillRoutes::getAuthRedirectPath());
}

return $next($request);
Expand Down
7 changes: 7 additions & 0 deletions src/TwillRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,11 @@ public function moduleShowWithPreview(
)
->middleware(['web', 'twill_auth:twill_users', 'can:list']);
}


public function getAuthRedirectPath(): string
{
return config('twill.auth_login_redirect_path')
?? rtrim(config('twill.admin_app_url') ?: '', '/') . '/' . ltrim(config('twill.admin_app_path') ?? 'admin', '/');
}
}
28 changes: 28 additions & 0 deletions tests/unit/Helpers/RouteHelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace A17\Twill\Tests\Unit\Helpers;

use A17\Twill\Facades\TwillRoutes;
use A17\Twill\Tests\Unit\TestCase;

class RouteHelpersTest extends TestCase
{
public function testGetAuthRedirectPath()
{
config(['twill.auth_login_redirect_path' => null, 'twill.admin_app_url' => null, 'twill.admin_app_path' => null]);

$this->assertEquals('/admin', TwillRoutes::getAuthRedirectPath());

config(['twill.admin_app_path' => 'twill']);

$this->assertEquals('/twill', TwillRoutes::getAuthRedirectPath());

config(['twill.admin_app_url' => 'https://admin.example.com/']);

$this->assertEquals('https://admin.example.com/twill', TwillRoutes::getAuthRedirectPath());

config(['twill.auth_login_redirect_path' => '/redirect']);

$this->assertEquals('/redirect', TwillRoutes::getAuthRedirectPath());
}
}