validate([ "token" => "required", ]); $caller_by = new Caller_DB(); $caller_by = $caller_by->where('token', $data['token'])->first(); if (!$caller_by) { $this->logRequest($request, null, 'failed', '401 Unauthorized', null); return response()->json(['error' => 'Unauthorized'], 401); } $this->logRequest($request, $caller_by, 'success', 'accepted command'); return response()->json(['success' => true], 200); } catch (\Illuminate\Validation\ValidationException $exception) { $this->logRequest($request, null, 'failed', '401 Unauthorized'); return response()->json(['error' => 'Unauthorized'], 401); } catch (\Exception $exception) { $this->logRequest($request, null, 'failed', $exception->getMessage()); return response()->json(['error' => 'An unexpected error occurred.' . $exception->getMessage()], 500); } } private function logRequest($request, $caller_by = null, $status, $error = null) { $logger = new CallerLog_DB(); if ($caller_by) { $logger->caller_id = $caller_by->id; $logger->caller_name = $caller_by->node_name; $logger->token_by = $caller_by->token; $manager = $caller_by->manager; } else { $logger->caller_id = "not responsible"; $logger->caller_name = "not responsible"; $logger->token_by = "not responsible"; } $logger->req_ip = $request->ip(); $logger->req_command = $request->path(); $logger->status = $status; $logger->error_exception = $error; $logger->save(); } }