header(self::AUTH_HEADER); $apiKey = ApiKey::getByKey($header); if ($apiKey instanceof ApiKey) { $this->logAccessEvent($request, $apiKey); return $next($request); } return response([ 'errors' => [[ 'message' => 'Unauthorized' ]] ], 401); } /** * Log an API key access event * * @param Request $request * @param ApiKey $apiKey */ protected function logAccessEvent(Request $request, ApiKey $apiKey) { $event = new ApiKeyAccessEvent; $event->api_key_id = $apiKey->id; $event->ip_address = $request->ip(); $event->url = $request->fullUrl(); $event->save(); } }