Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel Livewire V3 addHeaders Error #255

Closed
cavitznky opened this issue Dec 27, 2023 · 16 comments
Closed

Laravel Livewire V3 addHeaders Error #255

cavitznky opened this issue Dec 27, 2023 · 16 comments
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@cavitznky
Copy link

cavitznky commented Dec 27, 2023

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

@cavitznky cavitznky added bug Something isn't working unconfirmed Bug has not been reproduced yet labels Dec 27, 2023
@cavitznky cavitznky changed the title Laravel Livewire addHeaders Error Laravel Livewire V3 addHeaders Error Dec 27, 2023
@Kyon147
Copy link
Owner

Kyon147 commented Dec 29, 2023

@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.

@Kyon147 Kyon147 added help wanted Extra attention is needed question Further information is requested and removed bug Something isn't working unconfirmed Bug has not been reproduced yet labels Dec 29, 2023
@cavitznky
Copy link
Author

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
Copy link
Contributor

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
Copy link

cyberkishor commented Jan 21, 2024

@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
Copy link
Owner

Kyon147 commented Jan 24, 2024

@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
Copy link

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

@Kyon147
Copy link
Owner

Kyon147 commented Jan 24, 2024

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
Copy link
Owner

Kyon147 commented Jan 30, 2024

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
Copy link

donkooijman commented Feb 14, 2024

@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
Copy link
Author

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
Copy link
Contributor

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

@Kyon147
Copy link
Owner

Kyon147 commented Mar 12, 2024

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
Copy link
Contributor

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

@Kyon147
Copy link
Owner

Kyon147 commented Mar 25, 2024

This is the PR #286 for ref

@FrankWiebe
Copy link

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
Copy link
Owner

Kyon147 commented Jul 8, 2024

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

@Kyon147 Kyon147 closed this as completed Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

7 participants