diff --git a/app/Http/Responses/LoginResponse.php b/app/Http/Responses/LoginResponse.php new file mode 100644 index 0000000..484d465 --- /dev/null +++ b/app/Http/Responses/LoginResponse.php @@ -0,0 +1,46 @@ + '/admin', + 'free' => '/app', + ]; + + protected function shouldRedirect(Request $request, $redirect) + { + // Check if the current request path matches the redirect path + return !$request->is($redirect) && !$request->is($redirect . '/*'); + } + + public function toResponse($request) + { + setPermissionsTeamId(Auth::user()->current_team_id); + $user = Auth::user(); + + foreach ($this->roleRedirects as $role => $redirect) { + if ($user->hasRole($role)) { + return $request->wantsJson() + ? new JsonResponse(['two_factor' => false], 200) + : ($this->shouldRedirect($request, $redirect) + ? redirect()->to($redirect) + : redirect()->intended($redirect)); + } + } + + // Default redirection + $redirect = '/app'; + return $request->wantsJson() + ? new JsonResponse(['two_factor' => false], 200) + : ($this->shouldRedirect($request, $redirect) + ? redirect()->to($redirect) + : redirect()->intended($redirect)); + } +} diff --git a/app/Http/Responses/LogoutResponse.php b/app/Http/Responses/LogoutResponse.php new file mode 100644 index 0000000..4708545 --- /dev/null +++ b/app/Http/Responses/LogoutResponse.php @@ -0,0 +1,14 @@ + '/admin', + 'free' => '/app', + ]; + + protected function shouldRedirect(Request $request, $redirect) + { + // Check if the current request path matches the redirect path + return !$request->is($redirect) && !$request->is($redirect . '/*'); + } + + /** + * @param Request $request + * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse + */ + public function toResponse($request) + { + setPermissionsTeamId(Auth::user()->current_team_id); + $user = Auth::user(); + + // Check if the user has a role and redirect accordingly + foreach ($this->roleRedirects as $role => $redirect) { + if ($user->hasRole($role)) { + return $request->wantsJson() + ? new JsonResponse(['two_factor' => false], 200) + : ($this->shouldRedirect($request, $redirect) + ? redirect()->to($redirect) + : redirect()->intended($redirect)); + } + } + + // Default redirection + $redirect = '/app'; + return $request->wantsJson() + ? new JsonResponse(['two_factor' => false], 200) + : ($this->shouldRedirect($request, $redirect) + ? redirect()->to($redirect) + : redirect()->intended($redirect)); + } +} diff --git a/app/Providers/FortifyServiceProvider.php b/app/Providers/FortifyServiceProvider.php index 2d741e3..20928d3 100644 --- a/app/Providers/FortifyServiceProvider.php +++ b/app/Providers/FortifyServiceProvider.php @@ -6,12 +6,16 @@ use App\Actions\Fortify\ResetUserPassword; use App\Actions\Fortify\UpdateUserPassword; use App\Actions\Fortify\UpdateUserProfileInformation; +use App\Http\Responses\LoginResponse; +use App\Http\Responses\LogoutResponse; +use App\Http\Responses\RegisterResponse; use Illuminate\Cache\RateLimiting\Limit; use Illuminate\Http\Request; use Illuminate\Support\Facades\RateLimiter; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Str; use Laravel\Fortify\Fortify; +use Filament\Http\Responses\Auth\Contracts\LogoutResponse as LogoutResponseContract; class FortifyServiceProvider extends ServiceProvider { @@ -42,5 +46,9 @@ public function boot(): void RateLimiter::for('two-factor', function (Request $request) { return Limit::perMinute(5)->by($request->session()->get('login.id')); }); + + $this->app->bind(LogoutResponseContract::class, LogoutResponse::class); + $this->app->singleton(\Laravel\Fortify\Contracts\LoginResponse::class, LoginResponse::class); + $this->app->singleton(\Laravel\Fortify\Contracts\RegisterResponse::class, RegisterResponse::class); } }