PeterF's avatar

Laravel Sanctum: 403 This Action is Unauthorised

Trying to stay calm. But really, the quality of documentation in the Laravel space is doing my head in.

So I have created a vanilla laravel jetstream/livewire app. I made no decisions myself, just took every default offered to me. I have been using breeze for a few years, so it was time to upgrade my knowledge and app capability.....

So I made some migrations, got some tables up, seeded my standard user I use for testing, and then went and build a data entry form. Copying the pattern from the default dashboard route in the scaffolding, I created a route to my data entry page like this...

Route::middleware(['auth:sanctum', 'verified'])->get('/enter-trade', [TradeController::class, 'create'])->name('enter-trade');

Completely unremarkable. I login to the login page that comes with the scaffolding, press the button I added in, it calls this route, page is displayed. All as you would expect.

I then create an end point in my TradeController to accept the post from the form and copy the existing route and change it to....

Route::middleware(['auth:sanctum', 'verified'])->post('/save-trade', [TradeController::class, 'store'])->name('save-trade');

Basically the same, just a post instead of a get. Except that it constantly, returns,

403 This Action is Unauthorised

I have read things, watched videos on here, done it all... somehow, this just seems to be not working and I can't figure it out.

The only config I have done apart from what came out of the box, is adding two things, the first in resources/js/bootstrap.js

window.axios.defaults.withCredentials = true;

and in cors.php I changed false to true

  'supports_credentials' => true,

In the immortal words of Princess Leia... "Help me Laracasts, you're my only hope".....

0 likes
8 replies
Snapey's avatar

if you temporarily remove the middleware from the post route, does it reach your controller?

PeterF's avatar

@Snapey no.... I get the same thing when I change it to...

Route::post('/save-trade', [TradeController::class, 'store'])->name('save-trade');
Snapey's avatar

so not related to auth middleware

Do you have a form request class feeding data into the controller? Did you set the authorize() function to return true?

1 like
PeterF's avatar

@Snapey I have done nothing with an authorise() function at all.....

I have a blade file that does a POST to a route.... there is a @CSRF in the blade file, which is how I use to do things with breeze......

Snapey's avatar
Snapey
Best Answer
Level 122

@PeterF But what is in the controller? Did you make a FormRequest class?

1 like
PeterF's avatar

Thank you @snapey. I have been on quite the journey here, but have learnt a lot of things. When I created my resource controllers, I created form request objects for them all, with the plan of learning about that after I got a simple think working.... haha.... now I look inside one of them and the default is that the authorize() function just returns false..... Thanks for helping, very much appreciated... you just kept nudging me until I worked it out for myself..... Thanks. so much

Snapey's avatar

great. Don't forget to put the middleware back.

1 like
sachinkaru's avatar

@Snapey Thank you, sir. you are a lifesaver. I've been trying to fix this from long time.

Please or to participate in this conversation.