vuejs3laravel-9inertiajslaravel-jetstream

How to get 403 error message in Laravel Inertia as flash message?


Laravel 9 Jestream + Inertia + VUE3.

In the authorization policy, I return an error with the text

return Response::deny(trans('error'));

On the front, the Inertia modal window opens and I see the error text.

I added processing to Handler

 if ($response->status() === 403) {

     session()->flash('flash', [
         'bannerStyle' => 'danger',
         'banner' => $e->getMessage(),
     ]);

     return back();

 }

Now the Inertia modal window is not shown and I see the error text as a flash message. BUT. Now I don't get 403 errors if I made a request without inertia (axios library).

If I remove this code, returning 403 errors in axios requests works correctly (in vue as text). But with inertia requests, I again see a full-screen modal with a huge number 403. And I want to receive a small flash message. How can I do that??


Solution

  • You can probably check if the request is from Inertia or not. (covered in an answer to this question: https://stackoverflow.com/a/75934003/3650979 )

    In your handler you might do something like:

    if ($request->inertia() && $request->expectsJson() {
        if ($response->status() === 403) {
        }
    }
    // ## not tested ##