Hello to everyone, I wanted to add the "Unauthorized Access" Middleware for the Users that doesn't have the right permission to visit some web pages.
This is my middleware:
class CheckIfStaff
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if(Auth::user() && Auth::user()->is_staff === 1) {
return $next($request);
}
throw new NotStaffException();
}
}```
This is my Exception:
```class NotStaffException extends Exception
{
/**
* Render the exception into an HTTP response.
*
* @param \Illuminate\Http\Request
* @return \Illuminate\Http\Response
*/
public function render($request)
{
return response()->view('errors.not_staff', [], 403);
}
}```
And in /bootstrap/app.php:
` ->withMiddleware(function (Middleware $middleware) {
$middleware->append(CalculateTopUsersActivity::class);
$middleware->alias([
'is_staff' => App\Http\Middleware\CheckIfStaff::class
]);
})`
In web.php:
`Route::get('/create-staff', [FrontController::class, 'createStaff'])->name('staff')->middleware('is_staff');`
It doesn't work though. If I use "`$middleware->append(CheckIfStaff::class);`"
And the web.php in the same way I said, it would make the Middleware be global, so for every page I go, I would see error 403, from "IsNotStaff exception".. How I can solve this?