Skip to content

Commit

Permalink
update login logout flow based on role
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshKisb committed Sep 6, 2024
1 parent 5dc4256 commit 290ad07
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
46 changes: 46 additions & 0 deletions app/Http/Responses/LoginResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Http\Responses;

use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginResponse implements LoginResponseContract
{
protected $roleRedirects = [
'admin' => '/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));
}
}
14 changes: 14 additions & 0 deletions app/Http/Responses/LogoutResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Http\Responses;

use Filament\Http\Responses\Auth\Contracts\LogoutResponse as Responsable;
use Illuminate\Http\RedirectResponse;

class LogoutResponse implements Responsable
{
public function toResponse($request): RedirectResponse
{
return redirect('/login');
}
}
51 changes: 51 additions & 0 deletions app/Http/Responses/RegisterResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Http\Responses;

use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Laravel\Fortify\Contracts\RegisterResponse as RegisterResponseContract;
use Illuminate\Support\Facades\Auth;

class RegisterResponse implements RegisterResponseContract
{
protected $roleRedirects = [
'admin' => '/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));
}
}
8 changes: 8 additions & 0 deletions app/Providers/FortifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 290ad07

Please sign in to comment.