AuthorizationException throws MethodNotAllowed in redirect due to status code 302 #956
Replies: 1 comment 2 replies
-
Hey @lcamero I tried your code and it works, at least in Laravel 8. I am getting an 303 redirect, even with just a I am using a smiliar approach in my Inertia apps by default, e.g. also for the Are you sure you have applied the Inertia middleware to your If I change the changeRedirectCode method to just |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
Using Laravel as backend, I've implemented a ProductPolicy for my Product model, mapped the validations in the __construct() method within my ProductController using
this->authorizeResource(Product::class, 'product');
.My routes are using a
Route::resource('product', ProductController::class);
definitionAnd I've implemented a
if ($exception instanceof AuthorizationException) {}
validation within my \App\Exceptions\Handler render() method to catch my authorization errors to perform a redirect back with some errorsThe issue I'm having is that I've been testing my DELETE route using a user with no permission to DELETE by performing a
<Link method="delete" as="button" :href="route('product.destroy', product.id)">Remove</Link>
in my Vue template.This results in a MethodNotAllowed because the DELETE permission fires, gets caught by my handler and returns a 302, which then goes to my redirect (to the index route in my case) but since we need a 303 redirect instead, it opens a model with the exception message.
Now, I've read that Inertia handles the 303 status code conversion in the Middleware, so I'm assuming that my AuthorizationException is getting in the middle and I'm having to do some check in the request method to convert the status code to 303 myself, something like
Is it possible I'm handling this the wrong way? Should I be handling the exception differently so Inertia has time to convert my status code?
I'm just trying to validate the user has DELETE permissions on a resource by using the policy validation, but the Exception seems to be getting in the way of the redirect, which makes me have to manually validate the request method.
I did check redirecting back within the destroy() method in my controller, and that works as long as I don't throw the exception.
Beta Was this translation helpful? Give feedback.
All reactions