Kyon147 / laravel-shopify

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

Laravel Livewire V3 addHeaders Error #255

Closed cavitznky closed 2 months ago

cavitznky commented 9 months ago

Hi everyone,

I want to develop an application with Laravel-livewire v3 but the following code is not working.

When I checked, there is no addHeaders method in v3.

How can I solve this?

inside the ('shopify-app::partials.token_handler') file

if (window.Livewire) {
    // livewire
    window.livewire.addHeaders({
        'Authorization': bearer,
        'content-type': 'application/json',
        'X-Requested-With': 'XMLHttpRequest'
    });
}

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'addHeaders')

@Kyon147

Kyon147 commented 9 months ago

@izsoft I've never used Livewire, so I don't know the answer to this one sadly.

I'll leave it open and add the help wanted tag, so someone else who also used Livewire might be able to assit.

cavitznky commented 9 months ago

Thank you for your answer @Kyon147,

I hope there are friends who have solved this problem for Livewire v3 and I would appreciate if they can help.

By the way, the code I mentioned in the first message works on Livewire v2. So for Livewire v3 it is also necessary to add a token to each request. But I couldn't find how to do it.

d-shannon commented 8 months ago

Yes, it's possible:

        if (Livewire) {
            Livewire.hook('request', ({options}) => {
                options.headers['Authorization'] = `Bearer ${window.sessionToken}`;
                options.headers['Content-Type'] = 'application/json';
                options.headers['X-Requested-With'] = 'XMLHttpRequest';
            });
        }
cyberkishor commented 8 months ago

@d-shannon I have added livewire in our app and added, javascript code in the header but it is still not working.

here is the issue: https://www.awesomescreenshot.com/video/24223426?key=3f47b3eb8e254c6d596ea7c81e56d9f9

The auth user is not showing after the livewire update.

Please suggest me if you have any solution. Thanks in advance.

Kyon147 commented 8 months ago

@cyberkishor This is just a guess as I't not worked with Livewire but check your AppBridge object and see if it still has the right info and that host is being passed into it with the stores details.

9 times out of 10 - user being lost between pages or transitions is because of the host param being lost so appbridge can reinit.

cyberkishor commented 8 months ago

@Kyon147 The AppBride is ok and it's past the correct host, the issue is inside the Livewire config or something.

Kyon147 commented 8 months ago

If I have some time this week one evening I'll try and spin up a quick test and see if I can see the issue

Kyon147 commented 8 months ago

Had a look this weekend but not jump out at me yet, so need to do some more digging but will let you know

donkooijman commented 7 months ago

@izsoft It appears that the middleware setting is no longer present in the Livewire 3 configuration file. As a result, the 'verify.shopify' middleware was not applied in my case.

I resolved this issue by adding the following code to AppServiceProvider to ensure that the middleware is applied.

Livewire::setUpdateRoute(function ($handle) {
            return Route::post('/livewire/update', $handle)
                ->middleware(['verify.shopify']);
        });
cavitznky commented 7 months ago

Greetings,

Thank you for your support.

As @d-shannon said, the token_handler blade file needs to be updated.

Also as @donkooijman said or as I mentioned below, you need to specify the middleware in the service providers.

Livewire::addPersistentMiddleware(VerifyShopify::class);

@Kyon147

d-shannon commented 6 months ago

Yes confirming @izsoft comment. I updated the token_handler and applied the persistent middleware.

Kyon147 commented 6 months ago

Great work all, can someone create a PR for this? It will allow others to use Limewire v3 if they wish or we can add the steps into the wiki.

MarkusPayne commented 6 months ago

I created a PR with based on @d-shannon comment with an updated token_handler blade file

Kyon147 commented 6 months ago

This is the PR #286 for ref

FrankWiebe commented 6 months ago

I'm having trouble getting livewire to work. I've added this to the app service provider

Livewire::setUpdateRoute(function ($handle) {
    return Route::post('/livewire/update', $handle)
          ->middleware(['verify.shopify']);
});

Livewire::addPersistentMiddleware(VerifyShopify::class);

I've also added the changes in your PR. But livewire isn't making any round trips to the server. I can confirm the livewire styles and script is being loaded. But even AlpineJS isn't working. Any ideas?

Kyon147 commented 2 months ago

Closing this issue as it is getting too long with other issues. Please open new issues if needed.