Skip to content

Instantly share code, notes, and snippets.

@alfelric
Created November 27, 2021 04:08
Show Gist options
  • Save alfelric/455bc1baef235d806f64bf1d440040d2 to your computer and use it in GitHub Desktop.
Save alfelric/455bc1baef235d806f64bf1d440040d2 to your computer and use it in GitHub Desktop.
Verify Token Middleware
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class VerifyTokenMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
try {
$endpoint = 'http://localhost:8002/api/verify';
$client = Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => $request->header('Authorization')
]);
$response = $client->get($endpoint);
if ($response->status() === 200) {
return $next($request);
}
} catch (\Throwable $th) {
//throw $th;
}
return response()->json(['msg' => "You don't have privilege"], 401);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment