Complete the code to create a middleware that checks if a user is authenticated.
<?php namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; class Authenticate { public function handle(Request $request, Closure $next) { if (!auth()->check()) { return redirect()->route('[1]'); } return $next($request); } }
The middleware redirects unauthenticated users to the login route.
Complete the middleware code to allow only users with 'admin' role to proceed.
<?php
public function handle(Request $request, Closure $next)
{
if ($request->user()->role !== '[1]') {
abort(403);
}
return $next($request);
}
The middleware checks if the user's role is 'admin' to allow access.
Fix the error in the middleware that should check if the request method is POST.
<?php
public function handle(Request $request, Closure $next)
{
if ($request->method() !== '[1]') {
return redirect()->back();
}
return $next($request);
}
The middleware should allow only POST requests to proceed.
Fill both blanks to create a middleware that filters requests with a specific header and aborts if missing.
<?php
public function handle(Request $request, Closure $next)
{
if (!$request->headers->has('[1]')) {
abort([2]);
}
return $next($request);
}
The middleware checks for the 'X-Auth-Token' header and aborts with 403 if missing.
Fill all three blanks to create a middleware that filters requests by IP and method, aborting if conditions fail.
<?php
public function handle(Request $request, Closure $next)
{
if ($request->ip() !== '[1]' || $request->method() !== '[2]') {
abort([3]);
}
return $next($request);
}
The middleware allows only requests from IP '192.168.1.1' using POST method, aborting with 403 otherwise.