Kyon147 / laravel-shopify

A full-featured Laravel package for aiding in Shopify App development
MIT License
363 stars 107 forks source link

Auth::user() return null in AppInstalledEvent listener #313

Closed thanhnv37 closed 3 months ago

thanhnv37 commented 4 months ago

Hello,

I'm following the guide below to create a Listener for the event AppInstalledEvent, it will be used to send email to the shop owner when he install the app.

But in the handle() function, I can't get the shop owner email. I know that I can get the shop info from Users table with $shopId, but the email stored in this table is not a correct email of shop owner.

So, I tried using the API get shop https://shopify.dev/docs/api/admin-rest/2024-04/resources/shop#get-shop

But the method Auth::user() did not work in this case, it returned null value. Please check the code file below for more details.

Questions: can I get the ShopModel in this event?

Or do you have any suggestion for sending welcome email rather than listening to this event?

Any help is highly appreciated.

Thanks

<?php
namespace App\Listeners;

use App\Mail\AppInstalled;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Contracts\Queue\ShouldQueue;
use Gnikyt\ShopifyApp\Messaging\Events\AppInstalledEvent;
use Illuminate\Support\Facades\Log;

class SendEmailAppInstalledListener implements ShouldQueue
{
  use Queueable;
  /**
   * Create the event listener.
   *
   * @return void
   */
  protected Shop $shop;

  public function __construct()
  {
    $this->onQueue('default');
  }

  /**
   * Handle the event.
   *
   * @param  object  $event
   * @return void
   */
  public function handle(AppInstalledEvent $event)
  {
    $shop = Auth::user();
    $data = $shop->api()->rest('GET', '/admin/shop.json')['body']['shop'];

    Mail::to($data['email'])->send(new AppInstalled($data));
  }
}
Kyon147 commented 3 months ago

Auth::user would not be available much like a job does not have it. You'd have to use the userId that is passed into the event and get the user from that.

    /**
     * Create a new event instance.
     *
     * @param ShopId $shop_id
     *
     * @return void
     */
    public function __construct(ShopId $shop_id)
    {
        $this->shopId = $shop_id;
    }
thanhnv37 commented 3 months ago

Hello @Kyon147

Thank you for your reply. Yes, I read the sampled code in the topic below and know how to get User from the database with passed $shop_id. https://github.com/Kyon147/laravel-shopify/issues/241 But the email stored in the Users table is not a correct email.

That's why I had to use api to get correct user email.

Do you have any plan to support using Auth::user on the App Install event?

Kyon147 commented 3 months ago

Hey @thanhnv37

As Auth only works within the middleware and the events run a lot like jobs, it would not really be possible to support it.

It's easy to get the user from the DB based on the id provided so there is also little reason to as well.

Hope this answers your question.