skip to Main Content

I have one seperate database for saving logs
I have middleware that saves every request with response

 public function handle($request, Closure $next, $logType)
    {
        $response = $next($request);
        CreateLog::dispatch($request, $response, $logType)->onQueue(config('queue.queues.logging'));
        return $response;
    }

Everything related to auth progress works after every work complated. beacuse u used $response = $next($request);

'user_id' => auth()->id(),

Trying to save user_id like that. Everything is working perfect
But in prod server user_id is being null

In prod server project running using docker

2

Answers


  1. The user_id is likely null in the logs on your production server because the authentication middleware is not functioning properly or the user is not authenticated. Verify the authentication configuration, check if the user is authenticated before accessing auth()->id(), and ensure the session configuration is set up correctly in your production environment.

    Login or Signup to reply.
  2. you need to double check multiple things:

    • what session driver are you using?
    • what is the queue driver are you using?
    • is your middleware runs after session and auth middleware runs ( based on the configuration in kernal.php )

    based on your question and the problem is happening only on production so probably your issue is communication, your queue handler can’t access session data which is usually the case if you separate them into different modules and connections to improve performance so to achieve this you need to pass the information you need from one module to another so in your case

    public function handle($request, Closure $next, $logType)
        {
            $response = $next($request);
            $userID = auth()->id();
            CreateLog::dispatch($request, $response, $logType, $userID)->onQueue(config('queue.queues.logging'));
            return $response;
        }
    

    and in your CreateLog you store this userID and use it instead of depending on the auth

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search