bwdev1
bwdev1
FFilament
Created by bwdev1 on 12/5/2023 in #❓┊help
Different log-in behaviour depending on user role
If comes across a similar problem , here is my solution, credits to guy from this thread https://laracasts.com/discuss/channels/general-discussion/filament-redirect-unauthenticated-and-non-admin-users-from-admin-to-a-certain-other-route : run
php artisan make:middleware RedirectIfNotFilamentAdmin
php artisan make:middleware RedirectIfNotFilamentAdmin
app/http/middleware/redirectifnotfilamentadmin.php:
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Filament\Facades\Filament;
use Filament\Models\Contracts\FilamentUser;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

class RedirectIfNotFilamentAdmin
{
public function handle(Request $request, Closure $next)
{
$auth = Filament::auth();

if (!$auth->check()) {
return redirect(route('login'));
}

Auth::shouldUse(Filament::getAuthGuard());

/** @var Model $user */
$user = $auth->user();


$panel = Filament::getCurrentPanel();

if ($user instanceof FilamentUser) {

if ($user->role !== 'admin') {
return redirect(route('dashboard'));
}


if (!$user->canAccessPanel($panel) && config('app.env') !== 'local') {
return redirect(route('user.home'));
}
}

return $next($request);
}
}
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Filament\Facades\Filament;
use Filament\Models\Contracts\FilamentUser;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

class RedirectIfNotFilamentAdmin
{
public function handle(Request $request, Closure $next)
{
$auth = Filament::auth();

if (!$auth->check()) {
return redirect(route('login'));
}

Auth::shouldUse(Filament::getAuthGuard());

/** @var Model $user */
$user = $auth->user();


$panel = Filament::getCurrentPanel();

if ($user instanceof FilamentUser) {

if ($user->role !== 'admin') {
return redirect(route('dashboard'));
}


if (!$user->canAccessPanel($panel) && config('app.env') !== 'local') {
return redirect(route('user.home'));
}
}

return $next($request);
}
}
Register this middleware in the AdminPanelServiceProvider change: ...->authMiddleware([ Authenticate::class, ]); To use App\Http\Middleware\RedirectIfNotFilamentAdmin; ...->authMiddleware([ RedirectIfNotFilamentAdmin::class, ]);
16 replies
FFilament
Created by bwdev1 on 12/5/2023 in #❓┊help
Different log-in behaviour depending on user role
no worries
16 replies
FFilament
Created by bwdev1 on 12/5/2023 in #❓┊help
Different log-in behaviour depending on user role
so redirect causes an error
16 replies
FFilament
Created by bwdev1 on 12/5/2023 in #❓┊help
Different log-in behaviour depending on user role
this function expects a boolean return
16 replies
FFilament
Created by bwdev1 on 12/5/2023 in #❓┊help
Different log-in behaviour depending on user role
Youre right, and thats where my problem is - the default login behaviour is to try to redirect the user to the filament panel, so 'clients' are able to log in, but when they try to log in they are met with an error page. However if they navigate back to the homepage again, they will find themselves logged in. Hope that makes sense
16 replies
FFilament
Created by bwdev1 on 12/5/2023 in #❓┊help
Different log-in behaviour depending on user role
I've followed the documentation and kept it - i think it may be a bit reduntant as I only have 1 panel, so there is no need to check for its id, so i could get rid of it
16 replies
FFilament
Created by bwdev1 on 12/5/2023 in #❓┊help
Different log-in behaviour depending on user role
$this->role seems to work just fine in my code, i've logged it and it returns the correct 'role' when logging in as either client or admin
16 replies