-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update login logout flow based on role
- Loading branch information
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters