Skip to content
Advertisement

Laravel vapor “This action is unauthorized” with file upload

I have set up Laravel Vapor with my RESTFull app using Sanctum and now I’m trying to simply upload a file. I’m making a request POST /vapor/signed-storage-url and I’m getting:

JavaScript

I have created the UserPolicy as described in the docs:

JavaScript

But I keep getting This action is unauthorized.

Advertisement

Answer

The key piece of information here is that I’m using Sanctum to authenticate my users in my app. Laravel’s Vapor uses by default the web middleware

From the docs I wasn’t able to find a way to publish Vapor’s configuration.

If we look at the routes configuration we will have:

JavaScript

Vapor is getting the vapor.middleware environment to tell which middleware will be applied to /vapor/signed-storage-url route. Since I’m using Sanctum I just had to manually publish Vapor’s configuration by creating a vapor.php in my config folder:

JavaScript

Now in this file you can define your middleware to be set to auth:sanctum:

JavaScript

Now Vapor will start to use the auth:sanctum middleware to authenticate the request to POST /vapor/signed-storage-url

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement