Change LMS for Primaya jwt generate diluar
This commit is contained in:
47
app/Http/Middleware/JwtMiddleware.php
Normal file
47
app/Http/Middleware/JwtMiddleware.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Models\ApiClient;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
use App\Services\AuthService;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class JwtMiddleware
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
|
||||
*/
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
$token = $request->bearerToken();
|
||||
if (!$token) {
|
||||
return response()->json(['message' => 'Unauthorized!'], 401);
|
||||
}
|
||||
|
||||
$decoded = AuthService::verifyClientToken($token);
|
||||
if (!$decoded) {
|
||||
return response()->json(['message' => 'Invalid Token'], 401);
|
||||
}
|
||||
|
||||
// Identify client by sub claim
|
||||
$clientId = $decoded->sub ?? null;
|
||||
if (!$clientId) {
|
||||
return response()->json(['message' => 'Invalid client in token'], 401);
|
||||
}
|
||||
$clients = config('api_clients.clients');;
|
||||
$client = collect($clients)->where('api_key', $clientId)->first();
|
||||
if (!$client || ($client->is_revoked ?? false)) {
|
||||
return response()->json(['message' => 'Client not found or revoked'], 401);
|
||||
}
|
||||
// Attach client info to request
|
||||
$request->attributes->set('client', $client);
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user