laravel-shopify icon indicating copy to clipboard operation
laravel-shopify copied to clipboard

Unable to get Auth::user() in AfterAuthenticateJob

Open yasir-naseer-tetralogicx opened this issue 3 years ago • 4 comments

I tried registering after authenticate job just like we do it versions before 17, and then i tried to get Auth::user(), but it returns null

here is my job registration

'after_authenticate_job' => [ [ 'job' => \App\Jobs\AfterAuthenticateJob::class, 'inline' => env('AFTER_AUTHENTICATE_JOB_INLINE', true) ], ],

And i my job code is public function handle() { $user = Auth::user(); dd($user); // returns null

}

Hmm, we may need to mod this to directly input the user into the job.

gnikyt avatar Jul 13 '21 18:07 gnikyt

Sorry brain fart... @yasir-naseer-tetralogicx The shopdomain should be in the constructor already, you need to look the shop up based on this.

gnikyt avatar Jul 13 '21 18:07 gnikyt

@yasir-naseer-tetralogicx

Something like this:

public function __construct($shopDomain, $data)
{
  $this->shopDomain = $shopDomain;
  $this->data = $data;
}

public function handle()
{
  $user = User::where('name', $this->shopDomain)->firstOrFail();
  // do stuff here
}

ncpope avatar Jul 13 '21 20:07 ncpope

For me only 1 variable in constructor works

<?php
namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Helper;


class AfterAuthenticateJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; 
    protected  $shopDomain;
    protected  $data;

    protected  $shop;

    // public function __construct($shopDomain, $data)
    // {
      
    //   Log::info("Constructor - start- shopDomain".$shopDomain);
    //       Log::info($data);
    //   Log::info("Constructor - end");

    //   $this->shopDomain = $shopDomain;
    //   $this->data = $data;
    // }

    public function __construct($shop)
    {
      
      Log::info("Constructor2 - start");
          Log::info($shop);
      Log::info("Constructor2 - end");

      $this->shop = $shop;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Log::info("AfterAuthenticateJob-=call=start-".Date('Y-m-d h:i:s'));
        
        // Log::info("shopDomain".$this->shopDomain);

        // $this->shop = User::where('name', $this->shopDomain)->firstOrFail();

        Helper::createSearchPage($this->shop);
        
        Log::info("AfterAuthenticateJob-=call=end-".Date('Y-m-d h:i:s'));
    }
}

eng-abdurraoof avatar Nov 10 '21 10:11 eng-abdurraoof

Closing as a stale issue, $shopDomain is in the constructor for the later versions and this looks like it could be an older version of the package.

Kyon147 avatar Sep 11 '22 07:09 Kyon147