Skip to content

Commit

Permalink
Default login redirect to admin url
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofandel committed Apr 2, 2024
1 parent 8611299 commit f35602c
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/Facades/RouteHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace A17\Twill\Facades;

use Illuminate\Support\Facades\Facade;

/**
* @method static string getAuthRedirectPath()
*/
class RouteHelpers extends Facade
{
protected static function getFacadeAccessor(): string
{
return \A17\Twill\Helpers\RouteHelpers::class;
}
}
12 changes: 12 additions & 0 deletions src/Helpers/RouteHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace A17\Twill\Helpers;

class RouteHelpers
{
public function getAuthRedirectPath(): string
{
return config('twill.auth_login_redirect_path')
?? rtrim(config('twill.admin_app_url') ?: '', '/') . '/' . ltrim(config('twill.admin_app_path') ?? 'admin', '/');
}
}
3 changes: 2 additions & 1 deletion src/Http/Controllers/Admin/LoginController.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\RouteHelpers;
use A17\Twill\Http\Requests\Admin\OauthRequest;
use A17\Twill\Models\User;
use A17\Twill\Repositories\UserRepository;
Expand Down Expand Up @@ -81,7 +82,7 @@ public function __construct(
$this->config = $config;

$this->middleware('twill_guest', ['except' => 'logout']);
$this->redirectTo = $config->get('twill.auth_login_redirect_path', '/');
$this->redirectTo = RouteHelpers::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\RouteHelpers;
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 = RouteHelpers::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\RouteHelpers;
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(RouteHelpers::getAuthRedirectPath());
}

return $next($request);
Expand Down
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\RouteHelpers;
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', 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());
}
}

0 comments on commit f35602c

Please sign in to comment.