It is generally best practice to check for authentication in a separate middleware, rather than within the middleware that is responsible for checking roles. This is because authentication is a separate concern from authorization, and it is better to keep them separate.
For example, you could create an Authenticate middleware that checks for authentication, and then use that middleware in the route group that contains the SuperAdminRoleMiddleware middleware. This way, you can ensure that authentication is checked before authorization.
// Authenticate middleware
public function handle($request, Closure $next)
{
if (!Auth::check())
return redirect('login');
return $next($request);
}
// SuperAdminRoleMiddleware middleware
public function handle($request, Closure $next)
{
if (Auth::user()->hasRole('SuperAdmin'))
{
return $next($request);
}
return redirect('/');
}