Hi, I need some advice on the connection logic of the application I am currently working on and on the implementation I have already set up.
To put it simply, to access the application, one must be logged in. At the user level, there are 2 access levels:
- Administrators: who can access the application and the Filament dashboard;
- Clients who can access the application, but not the Filament dashboard.
I would like to use the Filament login form, which perfectly matches my expectations.
So, for that, I modified the Filament Login class and the toResponse() method to handle redirection based on whether the user is an administrator or a client.
public function toResponse($request): Redirector|RedirectResponse
{
$user = Filament::auth()->user();
$intendedUrl = session()->get('url.intended', '/');
if (str_contains($intendedUrl, '/admin')) {
if ($user->hasRole('super_admin')) {
return redirect()->to(Filament::getUrl());
}
return redirect()->to(route('home'));
}
return redirect()->to(route('home'));
}
The idea is to manage the redirection based on the base URL and the user role.
I also created a home route with a custom Middleware (RedirectIfNotAuthenticated) :
Route::get('/', function () {
return view('welcome');
})->middleware('custom.auth')->name('home');
class RedirectIfNotAuthenticated
{
public function handle(Request $request, Closure $next): Response
{
if (!auth()->check()) {
return redirect()->route('filament.admin.auth.login');
}
return $next($request);
}
}
Everything seems to be working correctly; the only "hiccup" is that the login URL remains tied to admin/login and I have the feeling we can't change it (or perhaps I just haven't found how...).
Does that seem correct to you? Or is there a more optimal approach?
Thank you in advance for your advice 🙂