Skip to content

[4.x] Fix #[Authorize] attribute doesnt trigger component exception hook - #10483

Closed
ghabriel25 wants to merge 2 commits into
livewire:4.xfrom
ghabriel25:fix/authorize-attribute-clean
Closed

[4.x] Fix #[Authorize] attribute doesnt trigger component exception hook#10483
ghabriel25 wants to merge 2 commits into
livewire:4.xfrom
ghabriel25:fix/authorize-attribute-clean

Conversation

@ghabriel25

Copy link
Copy Markdown
Contributor

Scenario

When using #[Authorize] attribute to perform authorization as pre-method execution. It doesnt trigger the component exception() hook.

class PostPolicy
{
    public function edit(User $user, Post $post)
    {
        return false;
    }
}
<?php

use App\Models\Post;
use Livewire\Component;

new class extends Component
{
    #[Authorize('edit', 'post')]
    public function save(Post $post)
    {
        //
    }

    public function exception($e, $stopPropagation)
    {
        $stopPropagation();

        dd($e->getMessage()); // not triggered
    }
}

Problem

Attribute call() handled by SupportAttributes::call() which is running before the actual method get called so any exception thrown will not get handled by component exception hook.

Solution

Delegate authorization to HandlesAuthorization trait and inside BaseAuthorize::call(), wrap the component before performing the authorization from that trait. This way, any exception thrown can be catched from component exception() hook. All method parameters resolution handled just like before.

Whats Changed

  • New HandlesAuthorization trait
  • Move AuthorizesRequests trait from Component to HandlesAuthorization trait
  • Added regression tests to make sure both works as expected either using #[Authorize] attribute or $this->authorize()

Fix: #10410

@ghabriel25
ghabriel25 marked this pull request as draft August 3, 2026 08:03
@ghabriel25
ghabriel25 force-pushed the fix/authorize-attribute-clean branch 2 times, most recently from 49afa5b to b5cfa77 Compare August 3, 2026 08:20
@ghabriel25
ghabriel25 force-pushed the fix/authorize-attribute-clean branch from b5cfa77 to 8b89bcc Compare August 3, 2026 08:32
@ghabriel25
ghabriel25 marked this pull request as ready for review August 3, 2026 08:41
@ghabriel25

Copy link
Copy Markdown
Contributor Author

Going to close this as I've found more simple solution rather than exposing new public method to the component. I'll submit the PR later

@ghabriel25 ghabriel25 closed this Aug 7, 2026
@ghabriel25
ghabriel25 deleted the fix/authorize-attribute-clean branch August 7, 2026 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[4.x] #[Authorize] attribute doesnt trigger component exception hook

1 participant