<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class RoleMiddleware
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next, ...$roles): Response
{
// Check if the user is authenticated
if (!auth()->check()) {
return redirect('/login'); // Redirect to login if not authenticated
}
// Check if the user has the required role
if (!in_array(auth()->user()->role,$roles)) {
abort(403, 'Unauthorized'); // Return 403 Forbidden if role doesn't match
}
return $next($request);
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]