Authorization on Livewire component

Hi,

I have a question: I use $this->authorize(‘can-do-something’) to secure my Livewire component. If I want to use it on all methods, (the entire component), do I need to repeat $this->validate() on top of every method? Or should I override the constructor? Or is there another way?

Thanks for helping out!

If you’re using livewire routes, make a middleware. If you’re calling livewire in a view, conditionally render it.

Hi,

I asked this question also on the discussion page on livewire github without getting any answer so I try also here.

Suppose i set a middleware on a full page component(ex auth), can I safely assume that will not be possible to call any action of that component if not logged in?

Similarly, if I call the authorize function on the mount method of a component, can I assume that will not be possible to call any action of the component if not authorized?

I am asking this because I am trying to figure out if is it possible to just set a guard via middleware or just on the mount method instead of authorizing any single action of the component

Use your middleware in the route directly (e.g.)

Route::get('/your-action', YourComponent::class)->middleare('your_middleware);

Hey,

If you’re not using LiveWire routes you can check for auth in the component itself.
This post I wrote should help you out :slight_smile: